0

I am trying to solve this simple algorithm that change a string depending on the first character, it always run on (If) even if it doesn't meet it requirement; i can just check the "freecodecamp" for answer but i want some one to explain to me why it never get to the else statement thanks

let translatePigLatin = str => {


let myArr = str.split("")
  let arr = [...myArr]
  if(arr[0] === "a" || "e" || "u" || "i" || "o"){
    return [...arr,"w","a","y"].join("")
  }else{
    let firstChar = arr.shift()
    arr.push(firstChar)
     console.log(arr)
    return [...arr,"a","y"].join("")
  }
 
}
console.log(translatePigLatin("algorithm"));
lkhaoua
  • 13
  • 2
  • Does this answer your question? [Check variable equality against a list of values](https://stackoverflow.com/questions/4728144/check-variable-equality-against-a-list-of-values) – VLAZ Jun 23 '20 at 20:21

2 Answers2

1
if(arr[0] === "a" || "e" || "u" || "i" || "o")

This is always true because it is comparing arr[0] to "a", then checking the truth value of string "e" etc. Those following values are always true. You need something like:

if(arr[0] === "a" || arr[0] === "e" || arr[0] === "u" || arr[0] === "i" || arr[0] === "o")

or

if("aeiou".includes(arr[0]))
mlibby
  • 6,567
  • 1
  • 32
  • 41
0

You just need rewrite your condition to the next:

if(arr[0] === "a" || arr[0] === "e" || arr[0] === "u" || arr[0] === "i" || arr[0] === "o")

you can extract condition to the method, it will be more readable