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 trialJacob Schwartz
313 PointsArrays Task 2 out of 2
I am having a little bit of trouble with the Arrays task 2 out of 2
I have the
var todo = ["Learn Swift","Build App","Deploy App"
good.
But, I don't know/remember how to put that into a numbering with println.
Thank you in advance!
var todo = ["Learn Swift","Build App","Deploy App"]
println = ["Learn Swift\n t\ Build App\n \t Deploy App\n \t"]
18 Answers
Jacob Schwartz
313 PointsOkay, so, how should I apply it to my code to make it work?
Steve Hunter
57,712 PointsSorry for the delay - my internet has been down.
If the question is asking you to print out the nuber of items in the array, the above line is fine.
So your code will have the array definition,
var todo = ["Learn Swift","Build App","Deploy App"]
then the output of the .count
method,
println(todo.count)
If that doesn't work, there's more to the question that I'm not aware of.
Hope that helps!
Steve Hunter
57,712 PointsWould, println("\(todo[0]")
give you something you can work with?
Steve Hunter
57,712 PointsYes - I just tried that,
So, you created an array of three (0, 1, 2) strings and called it todo
.
You then want to print this out. You've correctly called println()
but your parameters need a little work.
An array counts from zero, so the first element in the array is zero
. You access array elements by using your variable name, todo
, followed by the element number within square brackets. So, your first array element is todo[0]
which will give you ```"Learn Swift".
Inside the println()
do the same ... println("(\todo[0])")
.
You can work with that to finish the exercise, I think, else give me a shout.
Steve.
Jacob Schwartz
313 PointsSteve Hunter, I tried doing that, I may have done it wrong, but I tried to follow your example.
Steve Hunter
57,712 PointsShow me your code and I'll see what went wrong! :-)
Jacob Schwartz
313 PointsHere's the println: println("(\todo[0])", "(\todo[1])", "(\todo[0])")
Steve Hunter
57,712 PointsThe backslash comes before the bracket ...
println("\(todo[0])", "\(todo[1])", "\(todo[0])")
That should sort you out! :-)
Steve Hunter
57,712 PointsOr better, println("\(todo[0])", "\(todo[1])", "\(todo[2])")
Jacob Schwartz
313 PointsHere is my current code:
var todo = ["Learn Swift","Build App","Deploy App"]
println("(todo[0])" , "(todo[1])" , "(todo[2])")
Says in the Array I need to add the 'count.'
Also, thanks you so much for your time, just started learning, I don't mean to take up your time.
Steve Hunter
57,712 PointsRight, the println()
needs a backslash before the brackets, like:
println("\(todo[0])", "\(todo[1])", "\(todo[2])")
I'm not sure what you mean by "add the 'count'" but an array does have a property that can be accessed by dot-notation which will return the number of elements in the array. So, println(todo.count)
will show '3' as there are three elements in that array.
I hope that helps, else let me know exactly what the question is asking.
Steve.
Jacob Schwartz
313 PointsThe Error is saying this:
Bummer! You need to call the 'count' method on the array.
My code as it is right now:
var todo = ["Learn Swift","Build App","Deploy App"]
println("(todo[0])", "(todo[1])", "(todo[2])")
And, I do believe that the todo.count needs to be added in, I'm just not sure where
Steve Hunter
57,712 PointsFine ... what is the question it is asking you? I don't know what it is asking you to do.
Jacob Schwartz
313 PointsThe Question/Error it is asking me:
Bummer! You need to call the 'count' method on the array.
My code (the same):
var todo = ["Learn Swift","Build App","Deploy App"]
println("(todo[0])", "(todo[1])", "(todo[2])")
The output Error:
swift_lint.swift:7:9: error: consecutive statements on a line must be separated by ';' var todo.count = ["Learn Swift","Build App","Deploy App"] ^ ; swift_lint.swift:7:9: error: expected expression var todo.count = ["Learn Swift","Build App","Deploy App"] ^ swift_lint.swift:7:5: error: type annotation missing in pattern var todo.count = ["Learn Swift","Build App","Deploy App"] ^
Steve Hunter
57,712 PointsNo. What is the question - you have given me the result of your answer. What are you trying to "pass"? What's at the top of the screen n the code challenge?
Steve Hunter
57,712 PointsNo. What is the question - you have given me the result of your answer. What are you trying to "pass"? What's at the top of the screen n the code challenge?
Jacob Schwartz
313 PointsOh, sorry, the question at the top is
Print out the number of items in the todo array using println.
Jacob Schwartz
313 PointsOh, sorry, the question at the top is
Print out the number of items in the todo array using println.
Steve Hunter
57,712 PointsRight! So, we know that accessing the .count property tells us the number of items in the array.
Something like,
println(todo.count)