Skip to content

Instantly share code, notes, and snippets.

@thoughtpalette
Created December 9, 2019 15:06
Show Gist options
  • Save thoughtpalette/60060f2ecfeed9fb743763f132b52812 to your computer and use it in GitHub Desktop.
Save thoughtpalette/60060f2ecfeed9fb743763f132b52812 to your computer and use it in GitHub Desktop.
Get Vowels from String, Sort and return by Count
var test = "SAMPLE";
function solution(S) {
let stringArr = [...S.toLowerCase()];
let vowelArr = ['a','e','i','o','u'];
const vowelString = stringArr.filter(letter => vowelArr.includes(letter)).sort().join('');
let finalString = '';
vowelArr = vowelArr.map(vowel => {
return {
'vowel': vowel,
count: vowelString.split(vowel).length - 1
}
}).sort((a, b) => (a.count > b.count) ? -1 : 1)
// Check for ties
const highestCount = vowelArr[0].count;
const tiedVowels = vowelArr.filter(vowel => vowel.count === highestCount);
if (tiedVowels.length > 1) {
const sortedVowels = tiedVowels.sort((a, b) => (a.vowel > b.vowel) ? -1 : 1);
for (let i = 0; i < sortedVowels.length; i++) {
const plural = vowelArr[i].count > 1 ? 's' : '';
const newLine = (sortedVowels.length - 1 !== i) ? '\n' : '';
finalString = `${finalString}${vowelArr[i].vowel} appears ${vowelArr[i].count} time${plural}${newLine}`;
}
} else {
const plural = vowelArr[0].count > 1 ? 's' : '';
finalString = `${vowelArr[0].vowel} appears ${vowelArr[0].count} time${plural}`;
}
return finalString;
}
console.log(solution(test));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment