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

JavaScript Introducing the Practice

Practice Math session

https://w.trhou.se/qosynexyw3

So stuck with concatenation i cant move ... i can't seem to find specific help on the internet either

4 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Jason G ! I "forked" your workspace and took a look around. I noticed that in another question you asked what forking does. It creates a clone of the workspace. Normally, the workspace URL is private to you. Posting a snapshot link like you've done allows us to make a copy and view it. Otherwise, we'd get a "Bummer!" page :smiley:

On line 28 of math.js you seem to be mixing up what should go in quotes and what shouldn't. The variables shouldn't go inside of quotes. You want the values of those variables converted. For example:

let x = 3;
console.log(x);
// prints out 3
console.log("x");
// prints out the letter x

I changed your line 28 to the following and it now prints out what I'd expect:

let message = "<h1>" + "Math with the numbers "  + input1  + " and " + input2 + "</h1>";

Because the <h1> tag is also a part of the string and you don't need any variable values between it and the "Math with the numbers" you could shorten that to:

let message = "<h1>Math with the numbers "  + input1  + " and " + input2 + "</h1>";

However, there's an even easier way to write it, though you'd need to learn about JavaScript Template Literals. Treehouse has a great workshop on them and it's only 9 minutes long!

let message = `<h1>Math with the numbers ${input1} and ${input2}</h1>`;

Hope this helps! :sparkles:

Thank you Jennifer Nordell this was my original code https://w.trhou.se/57cnq3iqbt although i get the feeling its not the way tutors would prefer me to code within this practice session the questions are trying to get me to use string concatenation (but i just don't understand it)

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Jason G I forked that one just now and took a look. I think it looks fine, though you have a bit of unnecessary code. You made variables for message1, message2 etc, but you didn't really need to do that. Also, your calculations can be done right inside the string which means we can eliminate a total of 8 unnecessary variables and three document.write() calls.

Ideally, we want all the HTML on the same string so we only write once.

Here is my code for the calculations:

message += `<p>${input1} + ${input2} = ${input1 + input2}</p>`;
message += `<p>${input1} * ${input2} = ${input1 * input2}</p>`;
message += `<p>${input1} / ${input2} = ${input1 / input2}</p>`;
message += `<p>${input1} - ${input2} = ${input1 - input2}</p>`;

document.write(message);

Hope this helps! :sparkles:

Oh wow I never thought of that! Yes my code was messy, i knew it when i wrote it, i'm just glad the program worked i was a little excited :-) but your way so so much more concise. Thank you so much!

https://w.trhou.se/1ovd64u5dx the completed code!