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
Cory Norell
10,613 PointsWhy are there two addition operators?
Can someone explain to me why there are two addition operators rather than one in the code for the Mad Libs Challenge Revisited video?
Ex)
questionsLeft = ' [ ' + questions + ' questions left] ' ;
Also, would this be similar to string interpolation in other languages, or am I looking at this the wrong way?
Thank you in advance!
1 Answer
Jason Anello
Courses Plus Student 94,610 PointsHi Cory,
This is an example of string concatenation.
The overall goal is to create a string that looks like this: "[5 questions left]"
Except instead of hardcoding a number in that string we want to use whatever value is in our questions variable.
That means there's 3 parts to this, the initial '[', followed by the variable, and then the rest of the string we'd like in the output.
That's why 2 addition operators are needed. We're trying to add 3 things together.
It's very similar to adding 3 numbers together. 5 + 3 + 8
2 addition operators are required because you're trying to add 3 numbers together.
I don't know if I would say that it's similar to string interpolation but it's the same overall goal. You're trying to get the value of a variable into a string.
Cory Norell
10,613 PointsCory Norell
10,613 PointsAhhhh, I'm an idiot. Haha, I was looking at it as though the ' + question + ' was being contained by the rest of the string, like this:
"[ ' + questions + ' questions left] "
So I thought there were two addition operators within the single quotations. I was just looking at the breaks in the wrong places, and your answer made me realize they were in fact separate strings.
Thank you very much!