Breakfast of Champions explainer


#1

The aim of this puzzle: Find the index of your favorite cereal.

Walkthrough of the solution: The bestcereals variable stores a list of strings. To check if the favorite cereal, 'ant bran' is on the list, we can use bestCereals.includes(favorite). If it is on the list, we want to output its ranking. To get the index number of an array value, you can use the .indexOf() method. It will search the array starting from the beginning, and as soon as it finds the value, it returns the index of that value in the array. If it reaches the end of the array and the value wasn’t included, then it returns the number -1.

To get the index of the favorite, you replace the 'mosquito bits' string with the favorite variable.

Sample code solution:
(Tap below to reveal)

import { bestCereals } from 'grasshopper.store';

let favorite = 'ant bran';

console.log(bestCereals.includes(favorite)
    ?
    bestCereals.indexOf(favorite)
    :
    'Not on the list.');

JavaScript Concepts: Conditionals (ternary statement), console.log(), import, .includes(), indexOf()


#2