The aim of this puzzle: Use the .length
property to count the number of elements in an array
Walkthrough of the solution: The length
property is used to count the number of elements contained in an array. For example:
console.log(['cat', 'dog', 'rabbit'].length)
will print 3, as there are 3 elements in the array.
In this puzzle, the listOfIceCreams
array is imported from additional code.
On the next line, the numberOfFlavors
variable is declared. As we can tell from its name, this variable should hold the number of flavors contained in listOfIceCreams
.
To complete the puzzle, add .length
to the end of listOfIceCreams
, so the number of elements in the array is saved to numberOfFlavors
.
Sample code solution:
(Tap below to reveal)
import { listOfIceCreams } from 'grasshopper.treats';
let numberOfFlavors = listOfIceCreams.length;
console.log('There are ' + numberOfFlavors + ' flavors to choose from');
JavaScript Concepts: .length
, Arrays, Properties, Import Statements, Variable Declarations with Let, Console
Additional Code (hidden code that runs before the puzzle’s code):
const _list = ['vanilla', 'chocolate', 'cookie dough', 'mint chocolate chip', 'pistachio', 'cookies and cream', 'coffee'];
defineModule(
'grasshopper.treats',
{
listOfIceCreams: _list
}
);