If else statement does not work. Whale talk project

The following code below is the way that is correctly made.

//Whale talk program
let input = 'Murcielagos culpables'; //Input to translate
const vowels = ['a', 'e', 'i', 'o', 'u'];
let resultArray = [];


// i is the index of input and v is the index of vowels
for (let i = 0; i < input.length; i++) {
  for (let v = 0; v < vowels.length; v++) {
    if (input[i] === vowels[v]) {
      if (vowels[v] === 'e') {
        resultArray.push('ee');
      } else if (vowels[v] === 'u') {
        resultArray.push('uu');
      } else {
        resultArray.push(vowels[v]);
      }
    }
  }
}
console.log(resultArray);

The output here is exactly what is expected to.

I was trying to make this more compact so I can write the if-else statement just once without the else-if.
So I tried this

//Whale talk program
let input = 'Murcielagos culpables'; //Input to translate
const vowels = ['a', 'e', 'i', 'o', 'u'];
let resultArray = [];


// i is the index of input and v is the index of vowels
for (let i = 0; i < input.length; i++) {
  for (let v = 0; v < vowels.length; v++) {
    if (input[i] === vowels[v]) {
      if (vowels[v] === 'e' || 'u') {
        resultArray.push(vowels[v] + vowels[v]);
      } else {
        resultArray.push(vowels[v]);
      }
    }
  }
}
console.log(resultArray);

The log of this code is all the vowels repeated twice and I just wanted e’s and u’s to be repeated. Why this does not work?

Sorry if my English is not good, I still learning. I hope you can understand.

because this condition:

if (vowels[v] === 'e' || 'u') {

is always true. You never compare 'u' to anything, so JS simply evaluates non-empty strings as true:

if ('u') {
   console.log(true);
} else {
   console.log(false);
}
1 Like