First Impressions explainer

Aim of the puzzle:
Use the .indexOf() method to find the index of the first instance of each item in an array.

Walk through of solution:
The .indexOf() method returns the index of an item in an array. To use it, attach .indexOf() to the array to be searched and add the item to search for inside the parenthesis. If the item is in the array, .indexOf() returns its index. If the item occurs more than once, it returns the index of the first instance. If the item isn’t in the array, it returns -1.

In the code that you start off with, there is an array, words, and a for…of loop. The for…of loop is incomplete. In this puzzle, you’ll update the second console.log() so that it returns the index of each word in the words array.

To complete the puzzle, attach .indexOf() to the words variable that is inside the second console.log(). Add word as the argument. Now, when you run the code you’ll see the index of the first instance of each item in the array. Notice how .indexOf() for 'impressions' returns 0 for both instances even though it is in the array twice.

Sample code solution:

let words = [

for (let word of words){
    console.log( word + ' is at index: ' )
    console.log( words.indexOf(word) )

Javascript Concepts: Variable, Array, For…Of Loop, console.log(), .indexOf(), Array Methods


Thank you for this valuable piece of theory! @discobot

1 Like

Hi! To find out what I can do, say @discobot display help.