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 trialSpencer Bunting
1,144 PointsI am stumped on the first task in this challenge how do I convert the strings in this function to a tuple?
func greeting(person: String) -> (String) { let language = ("English") let greeting = ("Hello (person)")
return greeting
}
I understand how the tuple works within his example but I am unable to apply it to the challenge.
7 Answers
Vittorio Somaschini
33,371 PointsHello Spencer. I presume you are stuck in the 1/3 code challenge in the tuples section.
It should go like this: 1) before the curly braces we define what we are going to get, so we need to add: (language: String, greeting: String) 2) We need to change the return statement, from just return greeting to return language, greeting.
So final code would be:
func greeting(person: String) -> (language: String, greeting: String) {
let language = "English"
let greeting = "Hello \(person)"
return (greeting,language)
}
I hope this makes it clear
Vladimir Golikov
1,017 PointsHello Vittorio! Thank you for your answer. But i still can not understand my mistake. Here is my code.
func greeting(person: String) -> (language: String, greeting: String) {
let language = "English"
let greeting = "Hello \(person)"
return (language,greeting)
}
var result = greeting("Tom")
It works fine in playground on my Mac, but i still get an error here. :( I did not forget to put backslash before (person) for interpolation.
Vittorio Somaschini
33,371 PointsHello Vladimir.
It looks like for some reason in this last code you have provided you inverted the returned values (which you did not do in the previous pasted code).
If you swap them, greeting first then language you should pass it.
Vladimir Golikov
1,017 PointsCould you also help with the second task. I can not understand what does task want me to do. Look at my code:
func greeting(#person: String) -> (language: String, greeting: String) { let language = "English" let greeting = "Hello (person)"
return (greeting,language)
} var result = greeting(person:"Tom")
I declared a variable and asigned it to the tuple which returns after function works. But system still writes to me "Your result
variable has the wrong value in it. Check the task instructions again." What is wrong?
Vittorio Somaschini
33,371 PointsHello Vladimir Golikov .
I noticed an error in your code which is: "greeting = "Hello (person)". This should be instead "Hello (person)"" as we need String interpolation here.
The rest of the code looks ok and it should work after that correction.
I haven't used the # for this exercise, but if you then specify "person:" when calling the function it should work good too.
;)
Vladimir Golikov
1,017 PointsVittorio, thank you so much for help! Now its ok!
Arif Saputra
8,372 Pointsi stuck in third question, here is my code for print the language, but i still get an error
func greeting(person: String) -> (language: String,greeting: String) {
let language = "English"
let greeting = "Hello \(person)"
return (greeting,language)
}
var result = (greeting: "Tom",language: "English")
println("\(result.language)")
any help?
Sherrie Gossett
14,924 Pointsprintln(result.1)
Check out around the 6:17 mark in the video on Tuples.
Arif Saputra
8,372 PointsThanks sherrie, works like a charm :D
Vittorio Somaschini
33,371 PointsHello Christopher.
It would pass 1/3 in both cases. The order does not really matter for the first code challenge.
It matters for the 2/3 instead as we return a tuple, so more than 1 value basically and we want them to be in the right order inside this tuple.
This is the reason why returning (greeting,language) or (language,greeting) makes a difference.
Vittorio
NOUR A ALGHAMDI
Courses Plus Student 5,607 Pointsthanks Vittoria Somaschini , i really found an important information in your answers thank you :) you are Talented
Jaemin Yi
4,040 PointsJaemin Yi
4,040 PointsCan someone explain why the test passes if it's (greeting,language) but not if it's (language,greeting)?
I don't understand why the order would matter here, or why someone would change it from (language,greeting) in the first place. Can someone explain? Thank you!