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 trialRicardo Gonzalez
2,286 PointsHow am I suppose to remove a string from the array and then assign it to a variable?
Need help on to do this challenge.
var shoppingList = ["toothpaste","bread","eggs"]
var cart = shoppingList.removeAtIndex(1)
3 Answers
jne
12,613 PointsThis is a possible solution. You would first want to store the array value you want to a variable and then remove it from the array.
var shoppingList = ["toothpaste","bread","eggs"]
//Stores the the item from array in removedItem variable
var cart = shoppingList[1]
//Removes "bread" from the array
shoppingList.removeAtIndex(1)
//The array will now look like this: ["toothpaste", "eggs"]
Stepan Ulyanin
11,318 PointsWell removeAtIndex()
method returns the the item that is being deleted from the array so there should be no need to assign to the variable prior to removing
jne
12,613 PointsYouΒ΄re right, but it helps doing it this way for him to understand better.
Grant Meacham
2,706 PointsIt seems like you are going above and beyond the original question, which is why your functionally correct code is being marked as incorrect. The original Code Challenge asks you to create a variable named "cart", and assign it the value of the String "bread" using subscripting syntax, but does not mention removing it from the original array. So the code that the challenge is looking for is
var shoppingList = ["toothpaste","bread","eggs"]
var cart = shoppingList[1]
Ricardo Gonzalez
2,286 PointsOk I see. Thanks You'all for the help.
Stepan Ulyanin
11,318 PointsStepan Ulyanin
11,318 PointsSeems fine to me, you will remove
bread
and assign it to thecart
variable, what is the problem?