Skip to content

Instantly share code, notes, and snippets.

@TakesTheBiscuit
Created April 20, 2023 16:10
Show Gist options
  • Save TakesTheBiscuit/9d4450d67cf3a584f9add75176c1a1dc to your computer and use it in GitHub Desktop.
Save TakesTheBiscuit/9d4450d67cf3a584f9add75176c1a1dc to your computer and use it in GitHub Desktop.
Check for vowels in a string in javascript
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