Perennial Plants explainer

Aim of the puzzle:
Use the includes method .includes() to check if an array contains a specific item.

Walk through of solution:
The includes method .includes() checks if an array contains a specific element. It will return true or false depending on whether the item inside the parentheses is found.

In the code that you start off with, there are 2 arrays, perennials and annuals, a variable, plant, and an if statement. The value of plant is randomly selected from an array that uses the spread operator to copy the items from the perennials and annuals arrays and combine them into one. As it is, the if statement uses .includes() to check if 'pea' is in the perennials array. If it is, it will return true, the code will run and 'is a perennial!' will be printed. If it isn’t, the code won’t run. Since 'pea' is in the annuals array the code will never run. In this puzzle, you’ll change 'pea' so there is the possibility that the code runs.

To complete the puzzle, inside the parentheses of .includes(), change 'pea' to plant. Now, there’s a fifty-fifty chance that plant will be from the perennials array.

Sample code solution:
(Tap below to reveal)

let perennials = ['artichoke', 'asparagus', 'rhubarb'];
let annuals = ['tomato', 'carrot', 'pea'];

let plant = pickRandom([...annuals, ...perennials]);


if (perennials.includes(plant) === true){
    console.log('is a perennial!');

Javascript Concepts: Array, Variable, Spread Operator, Return, .includes(), String
Grasshopper Concepts: pickRandom()

1 Like

“=== true” in if statement seems unnecessary