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

Joshua Moten
Joshua Moten
2,171 Points

How come I can combine interpolation and concatenation

// 1. Attach this file -- math.js -- to the index.html file using a <script> tag

// 2. Add an alert to announce the program with a message like "Let's do some math!"
alert("Let's do some math!");

// 3. Create a variable and use the prompt() method to collect a number from a visitor
const getFirstNum = prompt("Enter a low number: ");

// 4. Convert that value from a string to a floating point number
const convertFirstNum = parseFloat(getFirstNum);

// 5. Repeat steps 3 and 4 to create a second variable and collect a second number
const getSecondNum = prompt("Enter a higher number: ");
const convertSecondNum = parseFloat(getSecondNum);

// 6. Create a new variable -- message -- which you'll use to build
//    a complete message to print to the document
//    Start by creating a string that includes <h1> tags as well
//    and the two input numbers. The string should look something like this:
//    "<h1>Math  with the numbers 3 and 4</h1>" where the two numbers are
//    the values input from the user. Use string concatenation to create this
//    and make sure you actually perform the math on the values by 
//    using the + symbol to add their values together
const addResult = convertFirstNum + convertSecondNum; 
const subResult = convertFirstNum - convertSecondNum; 
const divResult = convertFirstNum / convertSecondNum; 
const mulResult = convertFirstNum * convertSecondNum; 
const message = "<h1>Math with the numbers "+convertFirstNum+ " and " +convertSecondNum+"</h1>"

// 7. Add another string to the message variable. 
//    The string should look something like this after concatenation:
//    "3 + 4 = 7"
   +  `<br> ${convertFirstNum} + ${convertSecondNum} = ${addResult}`
// 8. Add a linebreak tag -- <br> -- to the message variable
  +`<br> ${convertFirstNum} - ${convertSecondNum} = ${subResult}`

// 9. Continue to add to the message variable to include strings
//    demonstrating multiplication, division and subtraction
//    For example:
//    "3 * 4 = 12"
//    "3 / 4 = 0.75"
//    "3 - 4 = -1"

 +`<br> ${convertFirstNum} * ${convertSecondNum} = ${mulResult}`
 +`<br> ${convertFirstNum} / ${convertSecondNum} = ${divResult}`

// 10. Use the document.write() method to print the message variable 
//     to the web page. Open the finished.png file in this workspace
//     to see what the completed output should look like

 document.write(message);

2 Answers

Rachel Johnson
STAFF
Rachel Johnson
Treehouse Teacher

Hey Joshua Moten , thanks for your question and for sharing your code!

The magic of Javascript is that we can combine interpolation and concatenation!

Interpolation can be thought of as a magical String. Rather than just a regular string that uses " or ' though, by using backticks, we can inject data or content into this magical string when it is created. But, at the end of the day, it is still a string - just with some extra data that got inserted.

Concatenation is the act of joining strings together to create a new string. Since the result of interpolation is a string, we can use concatenation to join interpolated strings!

So this section that you've got in your code:

const message = "<h1>Math with the numbers "+convertFirstNum+ " and " +convertSecondNum+"</h1>"

// 7. Add another string to the message variable. 
//    The string should look something like this after concatenation:
//    "3 + 4 = 7"
   +  `<br> ${convertFirstNum} + ${convertSecondNum} = ${addResult}`
// 8. Add a linebreak tag -- <br> -- to the message variable
  +`<br> ${convertFirstNum} - ${convertSecondNum} = ${subResult}`

// 9. Continue to add to the message variable to include strings
//    demonstrating multiplication, division and subtraction
//    For example:
//    "3 * 4 = 12"
//    "3 / 4 = 0.75"
//    "3 - 4 = -1"

 +`<br> ${convertFirstNum} * ${convertSecondNum} = ${mulResult}`
 +`<br> ${convertFirstNum} / ${convertSecondNum} = ${divResult}`

Is perfectly doable!

Joshua Moten
Joshua Moten
2,171 Points

awesome!!! thank you so much!!!