Sold Out explainer


#1

The aim of this puzzle: Remove the last item from an array and store it in a variable.

Walkthrough of the solution: Using the .pop() array method on an array will remove the last item from the array and return it. Because the item is returned, it can be saved to a variable.

In this puzzle, the iceCreams array is imported from 'grasshopper.treats'. It starts with 7 items, but we can remove some, one at a time, using .pop(). The favorite1 variable is storing the result of iceCreams.pop(). That means, take the last item off of the iceCreams array, and then store it inside favorite1. Now there are 6 items left in the iceCreams array.

favorite2 should store the new final item, the 6th in the array. This can be done the same way. Delete the empty string '', and replace it with iceCreams.pop().

Sample code solution:
(Tap below to reveal)

import { iceCreams } from 'grasshopper.treats';

console.log(iceCreams.length + ' flavors');

let favorite1 = iceCreams.pop();
let favorite2 = iceCreams.pop();

console.log('Sold Out:');
console.log(favorite1 + ' & ' + favorite2);

console.log(iceCreams.length + ' flavors');

JavaScript Concepts: Array Methods, .pop(), console.log(), Variable Declarations (let), import