0

I need help figuring out what is wrong with my code. I think the logic makes sense, but I'm not sure why it's not working.

function vowelsOnly(str) {
  var string2 = "";
  for(var i = 0; i < str.length; i++){
  if(str[i] === "a" || "e" || "i" || "o" || "u"){
    string2 += (str[i]);
  }
  }
  return string2;
}
rcarranza
  • 29
  • 4

3 Answers3

0

You need to fix the condition like so:

function vowelsOnly(str) {
  var string2 = "";
  for(var i = 0; i < str.length; i++){
  if(str[i] === "a" || str[i] === "e" || str[i] === "i" || str[i] === "o" || str[i] === "u"){
    string2 += (str[i]);
  }
  }
  return string2;
}
Arik
  • 5,266
  • 1
  • 27
  • 26
0

You need to repeat the complete condition everytime like this:

function vowelsOnly(str) {
  var string2 = "";
  for(var i = 0; i < str.length; i++) { 
      if(str[i] === "a" || str[i] === "e" || 
      str[i] === "i" || str[i] === "o" ||
      str[i] === "u") { 
      string2 += (str[i]); 
      }
   }
 return string2; 
 }
shim-kun
  • 123
  • 2
  • 10
0

another way is by regExp and replace method.

function vowelsOnly(str) {
 return str.replace(/[^aeiouAEIOU]/gi, '')
}
console.log(vowelsOnly("hello"));


function disemvowel(str) {
return str.replace(/[aeiouAEIOU]/gi, '');
}
console.log(disemvowel("hello"));
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35