-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 7 - CountVowelConsonant.js
70 lines (50 loc) · 1.46 KB
/
Day 7 - CountVowelConsonant.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*Count Vowel Consonant
https://scrimba.com/scrim/coc7c44a9a5601b43790f2982
DESCRIPTION:
You are given a string s that consist of only lowercase English letters.
If vowels ('a', 'e', 'i', 'o', 'u') are given a value of 1 and consonants are given a value of 2, return the sum of all of the letters in the input string.
Example:
For s = 'abcde', the output should be the countVowelConsonant(s) = 8
Hints: split(), reduce()
*/
function isVowel(character) {
const vowels = ['a','e','i','o','u']
return vowels.indexOf(character) == -1
}
function countVowelConsonant(str) {
const arr = [...str]
let sum = 0
for(i = 0; i < arr.length; i++) {
if(isVowel(arr[i])) {
sum = sum + 2
} else {
sum = sum + 1
}
}
return sum
}
// or
function isVowel(character) {
const vowels = ['a','e','i','o','u']
return vowels.indexOf(character) != -1
}
function countVowelConsonant(str) {
const arr = [...str]
return arr.reduce((previous, current) =>
isVowel(current) ? previous + 1 : previous + 2, 0)
}
/**
* Test Suite
*/
describe('countVowelConsonant()', () => {
it('returns total of vowels(1) and consonants(2) to be added', () => {
// arrange
const value = 'abcde';
// act
const result = countVowelConsonant(value);
// log
console.log("result: ", result);
// assert
expect(result).toBe(8);
});
});