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 trial

iOS Swift Basics Swift Types Recap: Swift Types

I am not a novice programmer by any means. The idea of combining something like "The product of" into an interpolated S

.....String is not well explained. I am trying to add the words without making that phrase a variable of its own. Please elaborate on how to do this. Thanks.....

types.swift
// Enter your code below

let firstValue = 20
let secondValue = 30
let product = firstValue * secondValue
let output = "\("The product of "), \(firstValue), \("times"), \(secondValue), \("is"), \(product)"
Greg Kaleka
Greg Kaleka
39,021 Points

BTW, since you're not a novice programmer, I'll also point you to the Swift documentation on string interpolation. Should help as well.

2 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Logan,

So "the product of" is just a mathematical term that means the result of multiplying two numbers together. The product of 5 and 2 is 10, for example. You already have the product of firstValue and secondValue stored in the constant product, so no more work to do there.

Secondly - you're doing way too much work in your interpolated string. There's no need to surround everything with parens. Interpolated strings are cool - you just write what you want the sentence to say except where you want to stick in a programmatic value like a variable or a mathmatical operation. For example:

let explanation = "The product of 5 and 2 is \(5 * 2), for example."
// this becomes "The product of 5 and 2 is 10, for example."

Neat, right? Let the computer do the math :blush:

So for this challenge you want to do basically the same thing, except 1. you want to use your firstValue and secondValue constants instead of hardcoding the numbers, and 2. You want to use the product constant instead of doing the math inside the string interpolation.

Really, you're quite close with your code. You've just got some extra \()s and commas and quote marks in there. A few strokes of the delete key and you'll have it.

Make sense? Let me know if you still can't figure it out!

Cheers :beers:

-Greg

Sweet thanks!!!

Sam Wight
Sam Wight
64 Points

See this answer for information about string interpolation in Swift. Your last line should look like this:

 let output = "The product of \(firstValue) times \(secondValue) is \(product)."
Greg Kaleka
Greg Kaleka
39,021 Points

Hi Sam! Looks like you're new around these parts. Welcome :blush:.

As a moderator here in the community, I try to discourage people from leaving answers like this - very little information other than the exact answer to how to solve the challenge. It's tempting to give fellow students the answer, but I think it's much better to lead them in the right direction, especially if they're already close to the answer on their own like Logan is.

Anyway, something to keep in mind. Thanks for participating in the community!

Sam Wight
Sam Wight
64 Points

Gotcha, thanks!