JS Whale Talk project step 5

In the project ‘Whale Talk’ ( https://www.codecademy.com/courses/introduction-to-javascript/projects/whale-talk ) step 5, we log 'inputIndex =' + inputIndex to the console, which prints each number of input.length, starting with 0. After this, we replace this code with console.log(input[inputIndex]) which replaces each index number with the corresponding letter in the input string. What function is this? How does it work? Does it turn ‘input’ into an array?

Full code:
const input='Can you help him find his son?'; const vowels=['a', 'e', 'i', 'o', 'u']; let resultArray=[]; for (let inputIndex=0; inputIndex<input.length; inputIndex++) { console.log(input[inputIndex]); }

2 Likes

input is declared as a string object, and remains a string. We access string indices the same way as arrays, with subscripts.

String[index]

Array[index]

Arrays and strings are zero-indexed which is why when we iterate over them, we exit when the iterator variable value reaches that of the length of the string or array.

2 Likes