The Last Laugh Puzzle explainer

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

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

In the code that you start off with, there is an array, laughter, a function declaration, theLastLaugh, and a function call, theLastLaugh('chortle'). theLastLaugh function takes a string as an argument. If that string has an index greater than -1 (meaning it exists in the laughter array), then the console will output its index. Otherwise, it will log '(word) is not in the array'. Immediately below this, the function is called using 'chortle' as an argument. Since 'chortle' is not in the laughter array, 'chortle is not in the array' gets logged to the console. In this puzzle, you’ll change the call to theLastLaugh to use the argument 'laugh'.

To complete the puzzle, inside theLastLaugh function call (), change 'chortle' to 'laugh'. Now, when you run the code you’ll see the index of the last instance of 'laugh'. Notice how .lastIndexOf() for 'laugh' returns 3 and not 0.

Sample code solution:

const laughter = ['laugh', 'chuckle', 'guffaw', 'laugh']

function theLastLaugh (str){
    if (laughter.lastIndexOf(str) > -1){
        console.log ('The last ' + str + ' is at index ' + laughter.lastIndexOf(str) )
    } else {
        console.log( str + ' is not in the array')
    }
};

theLastLaugh('laugh');

Javascript Concepts: Variable, Function, If Statement, Function Call, Array, String, console.log(), .lastIndexOf()