I'm trying to test if a variable does not equal multiple values but I don't want very long code

81 Views Asked by At

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 ().

1

There are 1 best solutions below

0
Alexander Nenashev On

Use Set to compare against it a character. Also place return answer in the end of the function otherwise you break the for loop prematurely on the first character:

function name(str) {
  const vowels = new Set([...'aeiou']);
  let answer = ""
  for (i = 0; i < str.length; i++) {
    if (!vowels.has(str[i])){
      answer += str[i]
    }
  }
  return answer;
}

console.log(name('asposdiusaeoasi'));

Spreading a string into an array and constructing a set is expensive, if you cache it (outside the function) the set solution beats regex:

` Chrome/121
----------------------------------------------------------
Set             1.00x  |  x100000  781  782  785  788  799
regex replace   1.63x  |   x10000  127  134  135  136  144
regex           2.37x  |   x10000  185  186  187  189  204
----------------------------------------------------------
https://github.com/silentmantra/benchmark `

const str = 'asposdiusaeoasi'.repeat(100);

const vowels = new Set([...'aeiou']);
function name(str) {
  let answer = ""
  for (i = 0; i < str.length; i++) {
    if (!vowels.has(str[i])){
      answer += str[i]
    }
  }
  return answer;
}

const vowelsRegex = /[aeiou]/;
function nameRegex(str) {

  let answer = ""
  for (i = 0; i < str.length; i++) {
    if (!vowelsRegex.test(str[i])){
      answer += str[i]
    }
  }
  return answer;
}
const vowelsReplace = /[aeiou]/g;
function nameReplace(str) {
  return str.replace(vowelsReplace, '');
}


// @benchmark Set
name(str);

// @benchmark regex
nameRegex(str);

// @benchmark regex replace
nameReplace(str);

/*@end*/eval(atob('e2xldCBlPWRvY3VtZW50LmJvZHkucXVlcnlTZWxlY3Rvcigic2NyaXB0Iik7aWYoIWUubWF0Y2hlcygiW2JlbmNobWFya10iKSl7bGV0IHQ9ZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgic2NyaXB0Iik7dC5zcmM9Imh0dHBzOi8vY2RuLmpzZGVsaXZyLm5ldC9naC9zaWxlbnRtYW50cmEvYmVuY2htYXJrL2xvYWRlci5qcyIsdC5kZWZlcj0hMCxkb2N1bWVudC5oZWFkLmFwcGVuZENoaWxkKHQpfX0='));