Aim of the puzzle: Use the
.includes() array method to check if an element exists inside an array.
Walk through of solution: This puzzle has 2 arrays,
heroes array has 3 elements, while
sidekicks is empty. These arrays should not be changed.
The starter code has the following ternary statement:
heroes === 'Batman' ? sidekicks.push('Robin') : console.log('Not found');
This checks if the 1st element in
'Batman'. If this is true,
'Robin' is pushed to the
sidekicks array. If it is false,
'Not found' is printed to the console.
'Batman' is in the
heroes array, but it is not at index
0, so the starter code prints
Fix the test by changing
This makes the test check the entire array for
'Batman', rather than just 1 position. This time,
'Batman' will be found, so when the code runs,
'Robin' will be pushed to the
Sample code solution:
let heroes = ['Luke Skywalker', 'Batman', 'Wonder Woman']; let sidekicks = ; heroes.includes('Batman') ? sidekicks.push('Robin') : console.log('Not found'); console.log(sidekicks);