본문 바로가기
알고리즘/Codewars

[Codewars] Vowel Count (7 kyu) / JavaScript

by fluss 2022. 10. 10.

https://www.codewars.com/kata/54ff3102c1bad923760001f3

 

Codewars - Achieve mastery through coding practice and developer mentorship

Coding practice for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Return the number (count) of vowels in the given string.

We will consider a, e, i, o, u as vowels for this Kata (but not y).

The input string will only consist of lower case letters and/or spaces.

 

설명:

주어진 문자열에서 모음의 수를 반환하세요.

이 카타에서 a, e, i, o, u를 모음으로 고려할 것입니다 (y는 아님).

입력 문자열은 소문자 및/또는 공백으로만 구성됩니다.

 

풀이

function getCount(str) {
  let result = str.match(/[aeiou]/g);
  if(!result) return 0;
  else return result.length;
}

입력된 문자열에서 모음이 존재하는지 확인하고 result가 null이면 0을 반환 아니면 result의 길이를 반환한다.

 

다른 사람의 좋았던 풀이

function getCount(str) {
  return (str.match(/[aeiou]/ig)||[]).length;
}

 

댓글