The aim of this puzzle: Check if each item in the shopping list is in the departments
array.
Walkthrough of the solution: The departments
array is imported from 'grasshopper.store'
. Then shoppingList
array is created. The last part of the code is the For Loop.
The loop goes through each item
of the shoppingList
. It logs some output for each item. Take a look at what it’s logging: item + ': ' + departments.includes('clothing')
1st is the item
which is just the current item in the shoppingList
. Then a ': '
which is just a string used to organize the output. The last part is departments.includes('clothing')
. That will check if the string 'clothing'
is an item of the departments
array. Instead of searching for 'clothing'
each time, it should check for the current item. Change this string to the item
variable.
Sample code solution:
(Tap below to reveal)
import { goods } from 'grasshopper.store';
let shoppingList = [
'clothing',
'food',
'books',
'toiletries'
];
for (let item of shoppingList) {
console.log(item + ': ' + goods.includes(item));
}
JavaScript Concepts: Binary Expression (+ concatenation), Code Block (for loop), Data Structures (array), import
, .includes()
Additional Code (hidden code that runs before the puzzle’s code):
let goods = ['books','clothing','electronics','furniture','sports','toys']