Write a function removeVowelKeys, that takes an array of strings keys and returns an array of keys that does not start with a vowel (aeiouy). The letter case does not matter.
Example :
removeVowelKeys(['alarm', 'chip', 'isValid', 'Advice', 'onClick']); // ['chip']
Here is my attmpt this is the max i could get out of it :
function removeVowelKeys(keys) {
let result = [];
for (let i=0;i<=keys.length;i++){
if (keys[i][0]!= 'a'||keys[i][0]!= 'e'||keys[i][0]!= 'i'||keys[i][0]!= 'o'||keys[i][0]!= 'u'||keys[i][0]!= 'y'){
result.push(keys[i])
}
}
return result;
}
The problem with this condition is :
if (keys[i][0]!= 'a'||keys[i][0]!= 'e'||keys[i][0]!= 'i'||keys[i][0]!= 'o'||keys[i][0]!= 'u'||keys[i][0]!= 'y')
that it will match for any alphabet. You are doing an OR check.
Take for example e and do a dry run. It will psas the first condition.
a will pass the second condition.
And if any one of the expression is true, the whole expression returns true.
function removeVowelKeys(keys) {
let result = [];
for (let i=0;i<keys.length;i++){
if (keys[i][0].toLowerCase()!= 'a'&&keys[i][0].toLowerCase()!= 'e'&&keys[i][0].toLowerCase()!= 'i'&&keys[i][0].toLowerCase()!= 'o'&&keys[i][0].toLowerCase()!= 'u'&&keys[i][0].toLowerCase()!= 'y'){
result.push(keys[i])
}
}
return result;
}
console.log(removeVowelKeys(['alarm', 'chip', 'isValid', 'Advice', 'onClick']));
Besides that, you want your code to run till i<keys.length. Arrays are 0 based right.
And to handle case, you would need to lower case as done above.
You can filter the array, and on each item check if the item startsWith a vowel character from the array with some method.
const vowelChar = ['a', 'o', 'e', 'i', 'y', 'u']
console.log(removeVowelKeys(['alarm', 'chip', 'isValid', 'Advice', 'onClick']));
// ['chip']
function removeVowelKeys(keys) {
return keys.filter(item => !vowelChar.some(c => item.toLowerCase().startsWith(c)))
}
Here is another solution just incase anyone wants to use regex. It still loops through the keys and grabs the first character of each passed argument, but it checks it against the vowels.
function removeVowelKeys(keys) {
let result = [];
for (let i = 0; i <= keys.length-1; i++) {
if(!keys[i][0].match(/[aeiuoy]/i)){
result.push(keys[i])
}
}
return result;
}
console.log(removeVowelKeys(['alarm', 'chip', 'isValid', 'Advice', 'onClick']));