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 trialCharles Williams
371 PointsTrying to Insert "Learn iOS" into 2nd position of array todo. Error 'todo' variable has the wrong value.
var todo = ["Learn Swift", "Build App", "Deploy App"] todo += (["Debug App","Fix Bugs"]) todo.removeAtIndex(1) let item = todo.removeAtIndex(1) todo.insert("Learn iOS", atIndex: 1)
Charles Williams
371 PointsThis worked fine in Playgrounds, but it didn't pass the challenge, so I'm not sure what's wrong.
2 Answers
William Li
Courses Plus Student 26,868 PointsAt first glance, there's issue with codes at line 3 & 4, as the part 2 of the challenge only asks for removing the 3rd from the Array and assign it to a constant, but line 3 of your code removed the 2nd item in the Array as well, which isn't what the challenge is looking for, and therefore fail to pass the grader check. (yeah, the grader do checks for whether todo has the correct length and items every time)
var todo = ["Learn Swift", "Build App", "Deploy App"]
todo += (["Debug App","Fix Bugs"]) // part 1 of 3
let item = todo.removeAtIndex(2) // part 2 of 3
todo.insert("Learn iOS", atIndex: 1) // part 3 of 3
Make sense?
Charles Williams
371 PointsOn part 2 of the assignment, I originally removed the 3rd from the Array, however, it was flagged as incorrect. I changed the value to remove the 2nd and it worked. That's why it is set to 1 instead of 2.
William Li
Courses Plus Student 26,868 Pointswithout seeing your original code, it's hard to pinpoint what went wrong. However, I should mention that you must write it in 1 line, remove item from Array and assign to a constant in one line let item = todo.removeAtIndex(2)
like such.
If you write it in two line, similar to the code you posted.
todo.removeAtIndex(2) // remove 2nd item from todo array
let item = todo.removeAtIndex(2) // remove 2nd item from todo array AGAIN!
This is wrong, because it removes 2 elements from the Array. Hope it helps.
Michael Reining
10,101 PointsI think the error is in a different line. Check the removeAtIndex. The value should be 2 not 1
This here worked for me:
var todo = ["Learn Swift", "Build App", "Deploy App"]
todo += ["Debug App", "Fix Bugs"]
let item = todo.removeAtIndex(2)
todo.insert("Learn iOS", atIndex: 1)
I hope that helps,
Mike
PS: Thanks to the awesome resources on Team Treehouse, I just launched my first app.
Danny Yassine
9,136 PointsDanny Yassine
9,136 PointsIs it this your trying to accomplish?
I tried it playground and it worked. No Errors. This is what I got in the console when printing the each statement above:
Missing anything to your question?