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

iOS Swift 2.0 Collections and Control Flow Introduction to Collections Working with Arrays

yusuf Dibswazit
yusuf Dibswazit
1,522 Points

what is wrong here

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.

var arrayOfInts = [1,2,3,4,5,6] arrayOfInts.append(55) arrayOfInts += [67] let value = arrayOfInts[4] let discardedValue = arrayOfInts[55] discardedValue.removeAtIndex(55)

as of Challenge Task 3 of 4

We also learned about reading values from an array. Retrieve the 5th item (remember array indexes start at 0) and assign the result to a constant named value.

when i pass this code: var arrayOfInts = [1,2,3,4,5,6] arrayOfInts.append(55) arrayOfInts += [67] let value = arrayOfInts[6] ITS WRONG ! BUT when i pass this code its becomes right : var arrayOfInts = [1,2,3,4,5,6] arrayOfInts.append(55) arrayOfInts += [67] let value = arrayOfInts[4]

how its the number 4 the 5th item, when it CLEARLY states that the indexes start with 0. so to my mind the number 6 should be the 5th item ! correct ??

also why is it when im writing a question its all stacked up neatly, but when its posted, its all in lines with No individual lines for every ENTER key i assign it to. ugggggru this is so time consuming and frustrating.

5 Answers

Marina Alenskaja
Marina Alenskaja
9,320 Points

Hey :-)

So, let's look at this together, step by step. The challenge says to retrieve the 5th value in the array and assign it to a constant named "value". Here are the index numbers for our 6 items and below the actual numbers for the items:

0 1 2 3 4 5

1 2 3 4 5 6

As you can see, the fifth item in the array corresponds with the index number 4.

I hope this helps you.

yusuf Dibswazit
yusuf Dibswazit
1,522 Points

hi, thank you very much, do you know what im doing wrong in task 4 of 4 ??

For challenge task 4 of 4, you should keep in mind:

  • To use the method removeAtIndex(), you need to put a dot after the array name, followed by the method name
  • To remove the 6th item from the array, you need to put the index number 5 in between the parentheses

Putting these together, you will get:

let discardedValue = arrayOfInts.removeAtIndex(5)

Hope this clears it up!

let discardedValue = arrayOfInts.remove(at: 5)

That's the correct answer. Just worked for me.

If I understand you correctly, you're asking for clarifications on the array indexing.

  • array[0] is the 1st item
  • array[1] is the 2nd item
  • ...
  • array[n] is the (n+1)th item

So, when you use arrayOfInts[6], you are asking for the 7th item.

Hope this helps!

let discardedValue = arrayOfInts.remove(at: 5)