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

can someone please help me here

const vowels = ‘aeiou’;

const newPhrase = phrase => {

const noVow = ;

for (let i = 0; i < vowels.length; i++) {

for (let j = 0; j < phrase.length; j++){

  if (vowels[i] === phrase[j]) {

   if (phrase[j] === 'u') {

    noVow.push('uu');

    }

    else{

      noVow.push(phrase[j]); 

}

}

}

}

return noVow;

}

console.log(newPhrase(‘turpentine and turtles’));

I get [ ‘a’, ‘e’, ‘e’, ‘e’, ‘i’, ‘uu’, ‘uu’ ]

From the above we can pencil out a solution…

[ 'uu', 'ee', 'i', 'ee', 'a', 'uu', 'ee' ]

which becomes,

'uueeieeauuee'

once the strings are joined.

how to pencil out I am not getting it can you please elaborate

and this should print ‘ee’

let vowels = ‘aeiou’;

const newPhrase = phrase => {
const noVow = ;
for (i = 0; i < vowels.length; i++) {
for (j = 0; j < phrase.length; j++){
if (vowels[i] === phrase[j]) {
noVow.push(phrase[j]);
}
if (phrase[j] === ‘u’ || phrase[j] === ‘e’) {
noVow.push(phrase[j]);
}
}
}
return noVow.join();
}
console.log(newPhrase(‘hey’));

but i get e,e,e,e,e,e

‘turpentine and turtles’
  ^  ^  ^ ^ ^    ^   ^
  ^  ^    ^      ^   ^
  u  e  i e a    u   e
  u  e    e      u   e

['uu','ee','i','ee','a','uu','ee']

['uu','ee','i','ee','a','uu','ee'].join('')  =>  'uueeieeauuee'