The aim of this puzzle: Add the updated smallTV
object back to the beginning of the televisions
array.
Walkthrough of the solution: The code starts by printing the televisions
array. Then the 1st element is shifted off and stored in the smallTV
variable. The .price
property is updated to be 5. We don’t want to set the price to $5, it should be set to half of its original value.
The number 5 should be changed to smallTV.price / 2
so that the code says, smallTV.price = smallTV.price / 2
.
Then the code uses the .unshift()
method to add the smallTV
to the beginning of the array.
Sample code solution:
(Tap below to reveal)
import { televisions, printTV } from 'grasshopper.store';
televisions.forEach(printTV);
let smallTV = televisions.shift();
smallTV.price = smallTV.price / 2;
televisions.unshift(smallTV);
console.log('Updated List:');
televisions.forEach(printTV);
JavaScript Concepts: Binary Expression (*), console.log()
, .forEach()
, import
, .shift()
, .unshift()
Additional Code (hidden code that runs before the puzzle’s code):
let televisions = [32, 40, 50, 55, 60].map(i => ({
size: i + `"`,
price: (i ** 1.5).toFixed(2)
}));
const printTV = tv => {
console.log(tv.size + ' for $' + tv.price)
};