Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Basics Swift Types Recap: Swift Types

Unsure 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

iOS Development with Swift Techdegree Student 285 Points

Hi 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
Alex Koumparos
Python Development Techdegree Student 36,862 Points

Hi 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,