Cutting the Line Explainer


#1

Aim of the puzzle: Use the .unshift() array method to add an element to the beginning of an array.
Walk through of solution: The .unshift() array method is used to add an element to an array. It is similar to .push(), except it adds an element to the front of an array rather than the end.

In this puzzle, the line array contains the strings 'Alice', 'Bobby', and 'Charlie'.
The variable newPerson contains the string 'Zeke'.

To complete the puzzle, use .unshift() to help Zeke get to the front of the line.

In between the 2 console.log statements, add .unshift(newPerson) to the end of line. This will add newPerson to the front of line.

Sample code solution:

let line = ['Alice', 'Bobby', 'Charlie'];
let newPerson = 'Zeke';
console.log(line[0] + ' is at the front of the line.');
line.unshift(newPerson);
console.log(line[0] + ' is at the front of the line.');

Javascript Concepts: Arrays, Array Methods, .unshift(), console.log