I'm trying to make a for loop that ignores lower case vowels. There are 5 in total and I don't want to write 5 'and' statements, because what if I have 10 values I don't want to equal? I would have to write 10 and/or statements?
Is there a quicker way?
function name(str) {
let answer = ""
for (i = 0; i < str.length; i++) {
if (str[i] !== "a" && str[i] !== "e" && str[i] !== "i" && str[i] !== "o" && str[i] !== "u") {
answer += str[i]
}
return answer
}
}
I don't want to do 5 && operators. Is there a way I can do something simple like this (which doesn't work):
if (str[i] !== ("a" && "e" && "i" && "o" && "u") {
answer += str[i]
}
return answer
I understand how to solve it, I just want to figure out a quicker or easier way to solve it, in case in the future I have 20 values I don't want to = for example. it feels primitive to do 20 && operators
I was expecting for the !== to check if str[i] was not equal too all the vales in the ().
Use
Setto compare against it a character. Also placereturn answerin the end of the function otherwise you break the for loop prematurely on the first character:Spreading a string into an array and constructing a set is expensive, if you cache it (outside the function) the set solution beats regex: