Sidekicks Explainer


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 and sidekicks. The heroes array has 3 elements, while sidekicks is empty. These arrays should not be changed.

The starter code has the following ternary statement:

heroes[0] === 'Batman' ? sidekicks.push('Robin') : console.log('Not found');

This checks if the 1st element in heroes is '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 'Not found'.
Fix the test by changing heroes[0] to:


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 sidekicks array.

Sample code solution:

let heroes = ['Luke Skywalker', 'Batman', 'Wonder Woman'];
let sidekicks = [];

heroes.includes('Batman') ? sidekicks.push('Robin') : console.log('Not found');

Javascript Concepts: Includes Array Method, Push Array Method, Ternary Statement, Variable Declarations with let, console.log