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;
}
CodePudding user response:
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.
CodePudding user response:
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)))
}
CodePudding user response:
Filter out the words which are starts with vowels. Simply use the Array.prototype.filter
method.
const removeVowelKeys = (arr) => {
const vowels = 'aeiou'.split(''); // make the vowels array from string.
// Now filter out the word which first character is not in the vowels array.
// Convert the first character to lower case for skipping the case sensitivity.
return arr.filter(word => !vowels.includes(word.charAt(0).toLowerCase()));
}
const res = removeVowelKeys(['alarm', 'chip', 'isValid', 'Advice', 'onClick']);
console.log(res);
CodePudding user response:
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']));
CodePudding user response:
I think this should do it.
converter(['alarm', 'chip', 'isValid', 'advice', 'onClick'])
const converter = (string) => {
let vowels = ['A', 'E', 'I', 'O', 'U', 'Y']
let newString = string.filter(x => !vowels.includes(x[0].toUpperCase()))
return newString
}
so, first we pass the params array of strings to function, then we create array of vowels to compare if in every first element of the array, we check if is vowel or not.
array.filter() will create new array base on your condition, so this will only return element whose first letter is not vowel.
CodePudding user response:
Array has a .filter
function that could be a best friend at this problem.
//Arrow function that checks whether string starts with vowel letter
const startsWithVowel = str => /^[aeiouy]/i.test(str);
//Main function that will filter incoming array of strings
const removeVowelKeys = arr => arr.filter(str => !startsWithVowel(str));
//calling our function to filter the array.
let filteredList = removeVowelKeys(['alarm', 'chip', 'isValid', 'Advice', 'onClick']);
//printing the result to console
console.log(filteredList)