scrimba
Solution for day 7 of #javascriptmas
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Solution for day 7 of #javascriptmas
AboutCommentsNotes
Solution for day 7 of #javascriptmas
Expand for more info
main.js
run
preview
console

function countVowelConsonant(str) {
const array = str.split('');
const vowels = ['a', 'e', 'i', 'o', 'u'];

// Set reducer function
function strReducer(accumulator, element) {
vowels.includes(element) ? accumulator += 1: accumulator += 2;
return accumulator;
}
// Reduce array
return array.reduce(strReducer, 0);

}


/*
// Alternative method iterating over array with for loop
function countVowelConsonant(str) {
const vowels = ['a', 'e', 'i', 'o', 'u'];
count = 0;
// Split str into array using split('')
for (letter of str.split('')) {
if (vowels.includes(letter)){
count += 1;
}

else {
count += 2;
}
}
return count;
}
*/



/**
* 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);
});
});
Console
"result: "
,
8
,
/index.html
LIVE