Whale Talk, multiple if's for Double e's and u's without Nesting

Hi Everyone,

I did this for the Whale Talk project and it works with the phrases I’ve put in. I watched the video and realized that in there they have if statements nested inside if statements. I understand how the solution works that way, but now I’m actually confused on how mine works. Link is below.

When I tried putting down else if instead of ifs for the double e’s and u’s, the code only produced single e’s and u’s. I think this comes down to how if’s work on there own versus else…if.

Any insight would be appreciated!

Hello o/
Basically, if statements do not effect other if statements in the same code block, and are run sequentially.
It might help to separate the if statements.
For example, these two code snippets are functionally identical:

if (input[i] === vowels[j]) {
    resultArray.push(input[i]); // <---- This code will run for any vowel
} if (input[i] === vowels[j] && input[i] === 'e') {
    resultArray.push(input[i]); // <---- This code will run for only 'e'
} if (input[i] === vowels[j] && input[i] === 'u') {
    resultArray.push(input[i]); // <---- And this will only run for 'u'
}
if (input[i] === vowels[j])
{
    resultArray.push(input[i]); // <---- This code will run for any vowel
} 

if (input[i] === vowels[j] && input[i] === 'e')
{
    resultArray.push(input[i]); // <---- This code will run for only 'e'
}

if (input[i] === vowels[j] && input[i] === 'u')
{
    resultArray.push(input[i]); // <---- And this will only run for 'u'
}

However while else if statements are also ran sequentially, they will only check the validity of the statement if the previous if statement was false.
So if we use ‘e’ as an example here’s what is happening:

if (input[i] === vowels[j]) { // <---- 'e' is a vowel so the statment is true
    resultArray.push(input[i]); // <---- Hence this code is ran
} else if (input[i] === vowels[j] && input[i] === 'e') { 
    // Since the previous if statment was true, this else if is skiped
    resultArray.push(input[i]);
} else if (input[i] === vowels[j] && input[i] === 'u') {
    // Like before, since the an if statment before it self was true, this else if is skipped.
    resultArray.push(input[i]);
}

Hopefuly that helped <3

1 Like

Yes this clears it up! Thank you.

1 Like