Aim of the puzzle: Use the
.lastIndexOf() array method to find the last index number of an element in array.
Walk through of solution: In this puzzle, the
lastIndexOf() array method is used to find the index number of the last appearance of an element in an array. It is the inverse of
.indexOf(), which finds the index number of the first appearance of an element in an array.
directions variable is an array of the strings
lastRight variable uses
.lastIndexOf() to find the position of the last
'R' in the array. To complete the puzzle, make the
lastLeft variable use
.lastIndexOf() to check for the last
'L' in the array.
Sample code solution:
let directions = ['L','R','R','L','R','L']; let lastRight = directions.lastIndexOf('R'); let lastLeft = directions.lastIndexOf('L'); console.log(lastRight); console.log(lastLeft);
lastIndexOf(), Variable Declarations with Let, console.log