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 trialAndrew Collazo
124 Pointslet name = "Andrew" let greeting = "Hi there, \(name)" let finalGreeting = "\(greeting), How are you?" whats wrong?
i keep writing this code
let name = "Andrew" let greeting = "Hi there, (name)" let finalGreeting = "(greeting), How are you?"
and it says im doing it correct on xcode but on teamtreehouse excersise its saying im wrong... please respond to me i never got a response last time.
let name = "Andrew"
let greeting = "Hi there, \(name)"
let finalGreeting = "\(greeting), How are you?"
1 Answer
andren
28,558 PointsThe problem is that you use string interpolation (merging variables into strings using \()
) instead of string concatenation, which is what the task explicitly asks you to use.
With string concatenation you combine strings and variables by using the +
operator. Both of these techniques where covered in the video preceding this challenge, and while they both result in the same string this task is specifically setup to test your knowledge of string concatenation and therefore only accept your code if you use that technique.
So to sum up you have to combine the greeting
variable and the " How are you?" string using the + operator, and not use \()
at all for the finalGreeting
value in order to pass this challenge.
Andrew Collazo
124 Pointscan you please show me the correct code and explain how you approached your answer?
The codes that I wrote are
-
let finalGreeting = "(greeting), How are you?" ///which computes to "Hi there ,Andrew, Andrew"
- let finalGreeting = greeting + "," + name///which also computes to "Hi there, Andrew, Andrew"
3.let rGreeting = "(greeting), How are you?"////which Strangely computes to "Hi there, Andrew, How are you?
please help I'm lost.
andren
28,558 PointsThe correct code is this:
let name = "Andrew"
let greeting = "Hi there, \(name)"
let finalGreeting = greeting + " How are you?"
As I stated in my previous answer by using + you can combine two different string together, since the greeting
constant contains the string "Hi there, Andrew" in order to get the requested string you can simply add " How are you?" to the end of that string, which is exactly what the + operator does.
Andrew Collazo
124 PointsAndrew Collazo
124 Pointsemail me at Andrew_Collazo@aol.com