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


challenge says I'm removing the wrong item from array but I'm certain that i removed the correct one?

A challenge for Swift says I'm removing the wrong item from the array but I'm certain that I'm removing the correct one! :-(,

heres the question: Challenge Task 4 of 4

Great job so far! Clearly you know how to add and read items from the array so in this challenge let's try getting rid of a value.

For this task, remove the 6th item from the array and assign the result to a constant named discardedValue.

To remove an item, use the method removeAtIndex() and put the index number in between parentheses that you want to remove.

Heres my code:

// Enter your code below

var arrayOfInts = [1, 2, 3, 4, 5, 6]


arrayOfInts += [8]

let value = arrayOfInts[4]


let discardedValue = arrayOfInts

What am I doing wrong? D:

2 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Jake Mcnaughton,

The problem is with the following code:

let discardedValue = arrayOfInts

When you remove an item from an array using the removeAtIndex() method, it returns a value that it removed for use. You can store it in a variable or constant. In this case, you're doing nothing with the returned value. Then, on the next line you're assigning your updated array to the constant discardedValue. However, what you really need to be assigning is the removed item.

This is the solution:

let discardedValue = arrayOfInts.removeAtIndex(5)

Good Luck

Oh I see it now!, thank you so much :)

Paschal Ihenacho
Paschal Ihenacho
914 Points

arrayOfInts.remove(at:5) let discardedValue = arrayOfInts[5]