-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathcountVowels.js
More file actions
41 lines (28 loc) · 786 Bytes
/
countVowels.js
File metadata and controls
41 lines (28 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
Write a function `countVowels` which takes a string as input and returns the count of vowels (both uppercase and lowercase) in the string.
What are vowels?
- Vowels are the characters: a, e, i, o, u (case-insensitive).
Example:
- Input: "hello world"
- Output: 3
- Input: "AEIOUaeiou"
- Output: 10
- Input: "xyz"
- Output: 0
- Input: ""
- Output: 0
Note:
- The function should count vowels in any alphanumeric string.
- It should handle empty strings gracefully.
Once you've implemented the logic, test your code by running
- `npm run test-countVowels`
*/
function countVowels(str) {
let s=str;
let cnt=0;
for (let ch of s.toLowerCase()){
if ("aeiou".includes(ch)) cnt++;
}
return cnt;
}
module.exports = { countVowels };