Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Arrays Store Multiple Values in an Array Remove Elements From an Array

Chris Roper
Chris Roper
1,632 Points

remove elements from an array challenge task JavaScript Arrays

Challenge Task 1 of 2

I'm sure I'm missing something really obvious but what???

script.js
//Challenge Task code
const orderQueue = ['1XT567437','1U7857317','1I9222528'];
//Declaring a new varible of an empty array named shipping
const shipping =[];
//Removing first item of the orderQueue array
orderQueue.shift();
//attempt 1 of adding the first item of the orderQueue array to shipping
const shipping = ['1XT567437'];
//attempt 2
shipping.unshift('1XT567437');
//attempt 3
shipping.unshift(orderQueue[0]);
//I've also tried adding the first element of orderQueue to shipping first. HELP!!!!!!!!!!!!!!

1 Answer

Joshua Ordehi
Joshua Ordehi
4,648 Points

Hey Chris,

The challenge can be a bit confusing because it says "declare a new variable" then goes on to say "remove the first element from orderQueue and assign it to the shipping variable"

However, it means to say you should do all of this in a single line of code while transferring the value from one array to another.

In your code, it looks like you're first declaring shipping as a constant, then trying to add a value to it. const creates an immutable variable, so trying to reassign it after declaration will return an Uncaught TypeError, more on that here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

When you did

orderQueue.shift();

You had the right idea, but you were just removing the first element from orderQueue, not assigning it to shipping. To do that, you'll want to declare the variable and assign the first element in the same line:

const shipping = orderQueue.shift();

I hope this helps! Godspeed and good luck in your learning journey!

Chris Roper
Chris Roper
1,632 Points

Thank you!

Thanks that makes perfect sense and seems really easy now! By using the same logic I managed to complete challenge task 2 on the first attempt.