Whale talk JavaScript || (OR) solution

hello, everybody.
i have a question.
i have finished the exercise with a else…if statement but codecademy suggest to do it with an OR operator in the if condition.
i started doing it out of curiosity but i dont know ho to finish the condition.
would be really happy if someone has an explanation.


const vowels =["a","e","i","o","u"];

const resultArray=[];

for(let i=0; i< input.length; i++){
  //console.log(input[i]);
 for( let j = 0; j < vowels.length; j++){
  // console.log(vowels[j])
  if(input[i]===vowels[j]){
   /* if( input[i]=== 'e'){
      resultArray.push('ee');
    }else if (input[i]=== 'u'){
      resultArray.push('uu')
      }else{
      resultArray.push(input[i]);
    }*/
    
    if(input[i]==='e'|| input[i] ==='u'){
  resultArray.push(input[i])
}else{
  resultArray.push(input[i])
}

  }
 }
}
console.log(resultArray.join('').toUpperCase());

Hello,

Since all vowels need to be on the array at least once you could do something else like this in your inner loop:

if (character is a vowel) {
  push the character to the result array
  if (the character is 'e' OR the character is 'u') {
    push the character to the result array again
  }
}

Obviously this isn’t working code, just trying to explain the logic of a possible solution. This would let you eliminate the else you currently have. A key part of this solution is to push the character you’re working with instead of hardcoding specific values. Once you know it’s a vowel, then you can use input[i] as the value to push.

2 Likes

I think this is the code block you are referring to? This is what I did and it works splendidly.

  if (input[i] === 'e' || input[i] === 'u') {
    resultArray.push(input[i]);
  }
1 Like

yes, but i dont understand the logic. how are you telling the code to double this two vowels with the OR operator???

you don’t. There are two possible ways to go about this:

always add the vowel to resultArray, then check if vowel is e or u, if so, push the vowel again. This is the flow illustrated by @selectall:

 if (character is a vowel) {
  push the character to the result array
  if (the character is 'e' OR the character is 'u') {
    push the character to the result array again
  }
}

the other flow is to use if/else like you currently do:

if(input[i]==='e'|| input[i] ==='u'){
  resultArray.push(input[i])
} else {
  resultArray.push(input[i])
}

but then you would need to push twice within the if clause.

2 Likes