Whats wrong with my whale talk solution?

Hi I just attempted the whale talk loop project and this was my attempt for a solution but the results seems to be wacky, can anyone explain to me what i got wrong in my code?

const input = 'turpentine and turtles'

const vowels = [ 'a', 'e', 'i', 'o', 'u']

let resultArray = [];

function whaleTalk(){
  for(let i = 0; i < input.length; i++ )
  for(let j=0; j < vowels.length; j++){
    if (input[i]===vowels[j]){
      resultArray.push(vowels[i])
    }
if (input[i]==='e' || input[i]==='u'){
      resultArray.push(vowels[i])
    }
let resultString = resultArray.join('').toUpperCase();
console.log(resultString)
  }
}

whaleTalk();

Missing open brace at end of that line. Check brace placement and be sure to close all that are open, and that they are placed correctly.

1 Like

Thank you looked again and tried brushing up on my code, but im still getting the wrong result array.

const input = 'turpentine and turtles'

const vowels = [ 'a', 'e', 'i', 'o', 'u']

let resultArray = [];


function whaleTalk(){
for(let i = 0; i < input.length; i++ ){
if (input[i] === 'e' || input[i] === 'u'){
    resultArray.push(input[i]);
  }

for(let j = 0; j < vowels.length; j++){
  if(input[i] === input[j]){
    resultArray.push(input[i]);
  }
}

}


console.log(resultArray)

}

whaleTalk();


result:


Output:
[
  't', 'u', 'u', 'r', 'p',
  'e', 'e', 't', 'e', 'e',
  't', 'u', 'u', 'r', 't',
  'e', 'e'
]

any suggestions?

Nvm found the solution, im dumb. thank you vm.

1 Like

am just wondering line
if(input[i] === input[j]) {
resultArray.push(input[i]);
}
instead of input[j] it should be vowel[j]

Hi @mtf, how are you doing? I hope you are great.

I have a doubt about loops, can you help me understand? It’s for sure silly but It’s making me uncomfortable moving on without knowing this.

When you set up a for loop like this:

for (let i = 0; i < 5; i++) {
    console.log(i);
}

We’re declaring a stop condition of is i < 5? I can understand why it works, and logs the numbers 0, 1, 2, 3, 4.

But when we’re setting up a loop like this:

const vacationSpots = ['Bali', 'Paris', 'Tulum'];

for (let i = 0; i < vacationSpots.length; i++) {
  console.log(`I would love to visit ${vacationSpots[i]}`);
}

How does the “i” knows that I’m trying to access the strings from the above array? When you imply vacationSpots.length on the stop condition is when the computer knows you want to work with the array declared above? Can you understand my doubt? I don’t know If I formulated my question right enough. If you can’t understand what I mean please tell me, I’ll try to clear it up.

This is often referred to as a stop condition but it is really a ‘while’ condition. The stop only takes effect when that condition is no longer true.

That given. we can explore the rest of your question when you affirm understanding of the above.

1 Like