whaleTalk project - need help understanding nested loop logic

I finished the whaleTalk javascript project but quite honestly I do not understand the logic of the nested loops. I do not understand what is happening with the nested vowelIndex loop (line 13) in relation to the inputIndex loop (line 5). I understand what each loop is doing on their own but I do not understand the logic of what is happening when the vowelIndex loop is running inside the inputIndex loop. That is the logic part I still do not understand.

Any help in explaining what is going on logic-wise would be greatly appreciated. Thank you! Code is below:

const input = 'turpentine and turtles is a nonsensical sentence'; const vowels = ['a', 'e', 'i', 'o', 'u']; const resultArray = []; for(let inputIndex = 0; inputIndex < input.length; inputIndex++) { //console.log('inputIndex is ' + inputIndex); if(input[inputIndex] === 'e') { resultArray.push(input[inputIndex]); } if(input[inputIndex] === 'u') { resultArray.push(input[inputIndex]); } for(let vowelIndex = 0; vowelIndex < vowels.length; vowelIndex++) { //console.log('vowelIndex is ' + vowelIndex); if(input[inputIndex] === vowels[vowelIndex]) { //console.log(input[inputIndex]); resultArray.push(input[inputIndex]); } } } //console.log(resultArray); const resultString = resultArray.join('').toUpperCase(); console.log(resultString);

The outer loop (line 5 in your codebyte) is being used to iterate over the characters of the input string. The index we are iterating over will be assigned to the loop variable inputIndex. We can use input[inputIndex] to access the character at that index.

In the body of the outer loop, we have two if conditions. If the character that we are iterating over is either 'e' or 'u', then a single 'e' or 'u' will be pushed to the resultArray.

Then we come to the inner loop (line 13) which is nested within the body of the outer loop. This loop will iterate over all the vowels i.e. it will iterate over the array ['a', 'e', 'i', 'o', 'u']. If the character of the outer loop (accessible as input[inputIndex]) is a vowel, then the corresponding vowel will be pushed to the resultArray.

Perhaps walking through the code a little bit may prove useful.

Suppose input is the string "turpentine and turtles is a nonsensical sentence".

  • First Iteration of Outer Loop:
    inputIndex will be 0 and input[inputIndex] will be 't'.
    The if conditions are false and nothing happens.
    The inner loop is executed. There will be 5 iterations of the inner loop (In the first iteration of the inner loop, vowels[vowelIndex] will be 'a', in the second iteration vowels[vowelIndex] will be 'e', in the third iteration vowels[vowelIndex] will be 'i', in the fourth iteration vowels[vowelIndex] will be 'o' , in the fifth iteration vowels[vowelIndex] will be 'u'.
    In each iteration of the inner loop, input[inputIndex] will be compared to one of the vowels. If there is a match, the character will be pushed to the resultArray. Since 't' is not a vowel, so the inner loop will finish without doing anything.
    resultArray is []

  • Second Iteration of Outer Loop:
    inputIndex will be 1 and input[inputIndex] will be 'u'.
    One of the if conditions is true, so a single 'u' will be pushed to the resultArray.
    resultArray is ['u']
    The inner loop is executed. There will be 5 iterations of the inner loop. The fifth vowel is 'u', so the if condition will be true in the last iteration of the inner loop and another 'u' will be pushed to the resultArray.
    resultArray is ['u', 'u']

  • Third Iteration of Outer Loop:
    inputIndex will be 2 and input[inputIndex] will be 'r'.
    The if conditions are false and nothing happens.
    The inner loop is executed and will finish without anything happening since 'r' is not a vowel.
    resultArray is still ['u', 'u']

  • … And so on …
    When the character is a not a vowel, nothing happens to resultArray.
    If the character is 'e' or 'u', two e’s or u’s will be pushed to resultArray (the first push because of the if conditions before the inner loop, the second push because of the if condition in the nested loop).
    If the character is a vowel 'a' or 'i' or 'o', then a single 'a' or 'i' or 'o' is pushed to resultArray because of the if condition in the nested loop.

Hi mtrtmk,

Thank you for taking time and thoroughly explaining the logic of the code. I honestly have gone back a few lessons and am reading other supplemental material to better understand the logic side of the lessons I have done so far. I am almost getting to the loop lessons again. Will definitely and carefully read every single line of explanation you shared with me. I really do appreciate you taking time out of your busy schedule and thoroughly answering my question/s. Will update you soon.

Thank you again!!!