Starting Small explainer

The aim of this puzzle: Remove the first 2 elements from the array.

Walkthrough of the solution: The televisions variable is an array of objects. If you wanted to remove one from the array, you could use .pop(), but that would take it from the end of the array. If you want to “pop” off the 1st element, you can use .shift() instead.

The starter code already shifts off the 1st element and prints it out. The original array is now 1 item smaller. To remove and print the new 1st element, you can use televisions.shift() and put that inside of printTV().

Sample code solution:
(Tap below to reveal)

import { televisions } from 'grasshopper.store';

function printTV(tv) {
  console.log(tv.size + ' for $' + tv.price);
}

televisions.forEach(printTV);
console.log('The smallest TV:');
printTV(televisions.shift());
console.log('The next smallest TV:');
printTV(televisions.shift());

JavaScript Concepts: Binary Expression (+ concatenation), console.log(), .forEach(), import, .shift()
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)
}));

In this puzzle, instruction says “below the second console.log() statement”. Note that the first is within the printTv function call. If you place your line of code under the second console log from the top, it will fail. In this exersise, the second refers to the last.

2 Likes

Hey there, thanks again for flagging!

Ben

1 Like

The example solution code gives “Solution code failed unexpected token *”

Hello, can you post a screenshot of the hidden code of this exercise. I am trying to do the same thing in playground but i can’t figure out how to do something like the imported variable you made. I do not know if i missed something in the past exercises.

I don’t think I can screenshot it, but here is the hidden code:

let televisions = [32, 40, 50, 55, 60].map(i => ({
    size: i + `"`,
    price: (i ** 1.5).toFixed(2)
}));

Hope this helps,
-Friidays :blush:

1 Like

Thank you so much, you are a legend🙏


Need a little help please, as you can see in the line of " televisions.foreach(printTv)"
I could not understand from where this printTv variable cama from. And i tried to print the variable and the result is in the screenshot. Kind regards.