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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Removing Items from an Array

Andrea Boscolo
Andrea Boscolo
7,250 Points

Why my code is not working???

I just don't get it.

script.js
var orderQueue = ['1XT567437','1U7857317','1I9222528'];
var shipping = orderQueue.shift();
orderQueue.pop(shipping);
var cancelled =orderQueue.pop();
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
Andrea Boscolo
Andrea Boscolo
7,250 Points

this is from a code challenge into the course JavaScript Loops, Arrays and Objects. The ask is: "Now create a new variable named cancelled. Remove the last item from the orderQueue array and store it in the variable cancelled. Use the array method you learned in the last video for this challenge."

3 Answers

Steven Ang
Steven Ang
41,751 Points

Everything is perfect except the piece of code on line 3, it's not needed for the challenge, just simply remove it.

Andrea Boscolo
Andrea Boscolo
7,250 Points

It was the request before this one, I can't delete it. I don't get why when I press check work a pop up error appears telling me the code is wrong.

Steven Ang
Steven Ang
41,751 Points

Click the restart button, just copy paste your code and remove the code on line 3.

Andrea Boscolo
Andrea Boscolo
7,250 Points

Now it works! thanks! but I don't still understand why I have to delete the third line of code..

Steven Ang
Steven Ang
41,751 Points

That piece of code removes the first item from the array again which is not what the challenge wants. The challenge wants you to store the first item into a new variable named shipping which you've done on line 2. But, then you used the shift method again on the array again which like I said remove the current first item in the array: '1U7857317'. So now the array looks like this:

"1I9222528" // You can test it at the chrome's console if you want

If my explanation is not clear enough, please respond back, I will try my best to explain better again.

Andrea Boscolo
Andrea Boscolo
7,250 Points

Thanks, I finally understood :)