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 trialManos Semertzides
245 PointsUnsure what step2 is asking
Step2 in this exercise is very vague. Asking for some support in what the output is looking for.
// Enter your code below
let firstValue = 12
let secondValue = 15
let product = \(firstValue) * \(secondValue)
let output = \(firstValue) * \(secondValue) = \(product)
2 Answers
dandan1987
iOS Development with Swift Techdegree Student 285 PointsHi Manos,
I believe Step 2 is asking you to write out a String statement such as:
let output = "The product of \ (firstValue) times \ (secondValue) is \ (product)"
Keep up the good work :)
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Manos,
You have some invalid syntax in this line:
let product = \(firstValue) * \(secondValue)
The construct \(someVariableName)
is only used inside strings and is used to tell Swift to evaluate the the code inside the parentheses and then turn the result of that evaluation into a string inside the rest of the string. This is called "string interpolation".
Outside of strings, you can just use variables normally. So, for example, if you wanted to add two numbers, firstNumber
and secondNumber
together, you could do this:
let sum = firstNumber + secondNumber
Hopefully this will get your product
line working.
Next, we have the output
line. This is where you do need to use string interpolation. Let's start by looking at the question to see what it wants the string to look like:
The string should read: "The product of 2 times 4 is 8".
Therefore, you need to end up with a string that has exactly that structure. Let's go back to the previous example to see what that might look like. Suppose we wanted to make a string that expressed in a sentence the sum of numbers firstNumber
and secondNumber
(and we'll assume we have the sum
constant from the previous example), we could write it like this:
let text = "The sum of \(firstNumber) and \(secondNumber) is \(sum)"
Look closely at the structure, we have a single string (denoted by the double quote marks). Inside that string, any values we want to evaluate are using the string interpolation syntax (denoted by the \( ... )
).
Hope that clears everything up for you.
Happy coding,
Alex