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 trialRaymond Espinoza
1,989 Pointsswift types
How do you get the firstValue = 2 and second value =4 to equal 8 and put it into string interpolation ?
// Enter your code below
let firstValue = 2
let secondValue =4
Raymond Espinoza
1,989 Pointsi know that but how do i compute the product and print out the product in a formatted string?The string should read: "The product of 2 times 4 is 8".
1 Answer
Steve Hunter
57,712 PointsHi Raymond,
It is correct that you need to interpolate strings to complete this exercise. The word interpolate means to insert. Adding strings together is generally termed concatenation - other, simpler, words are available!
You have started by creating your two values, firstValue
and secondValue
. That's great. Not, you want to create a further constant called product
which holds firstValue * secondValue
. Got that? Something like:
let product = firstValue * secondValue
You then need to output all that in a string. You're given the string in the challenge: "The product of 2 times 4 is 8". Now we use interpolation! The 2, 4 and 8 in that string are all held in constants in our code. We can insert them by using a backslash, brackets and the variable name - just like in the videos. Assign that new string into another constant called output
. The final code looks like:
let firstValue = 2
let secondValue = 4
let product = firstValue * secondValue
let output = "The product of \(firstValue) times \(secondValue) is \(product)"
Let me know if that doesn't work for you!
Steve.
Roman Lopez
2,211 PointsRoman Lopez
2,211 PointsHi Raymond I'll like to give this a try...
In this question the keyword you are looking for is "interpolation". When you interpolate two strings. You add the strings together.
Swift is so smart that with the constant name along is enough for Xcode to recognize its value! :) I hope it helps.
-roman