Created
April 20, 2023 16:10
-
-
Save TakesTheBiscuit/9d4450d67cf3a584f9add75176c1a1dc to your computer and use it in GitHub Desktop.
Check for vowels in a string in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const input = 'Hello World'; | |
const filterArr = ['a','e','i','o','u']; | |
let counted = getCountFromFilteredInput(input,filterArr); | |
// 3 | |
console.log(counted.length); | |
// ["e", "o", "o"] | |
console.log(counted); | |
// Passed | |
if (assertSame(counted.length, 3)) { | |
console.info('Passed'); | |
} | |
// assertions test | |
function assertSame(x,y) { | |
if (x===y) { | |
return true; | |
} | |
console.error('Failed assertion. Expected: ', x, ' to be: ', y); | |
return false; | |
} | |
// main function | |
function getCountFromFilteredInput(inputString, filterArr) { | |
let count = 0; | |
const inputStringSplit = inputString.split(''); | |
const checkerFunc = value => | |
filterArr.some(element => value.includes(element)); | |
return inputStringSplit.filter(checkerFunc); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment