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

Melissa Preece
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Melissa Preece
Full Stack JavaScript Techdegree Graduate 18,394 Points

Hi I tried attaching the math.js file to the index.html file but when I previewed it, the alert didn't pop up. Help!

At first I thought I didn't write the code correctly, but then I did and still the alert didn't show up. I have no way of testing if any further coding I do will work.

Can you post your code?

Melissa Preece
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Melissa Preece
Full Stack JavaScript Techdegree Graduate 18,394 Points

I couldn't do all the numbers below because I couldn't test if any of my code work. Not sure what I'm doing wrong...hope you can help, thanks in advance! :)

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

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

// DONE 3. Create a variable and use the prompt() method to collect a number from a visitor var userInput1 = prompt("Type a number.");

// DONE 4. Convert that value from a string to a floating point number userInput1 = parseFloat(userInput1);

// DONE 5. Repeat steps 3 and 4 to create a second variable and collect a second number var userInput2 = prompt("Type another number."); userInput2 = parseFloat(userInput2);

// DONE 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 var message = "<h1>Math with the numbers " + userInput1 + " and " + userInput2 + "</h1>";

// 7. Add another string to the message variable. // The string should look something like this after concatenation: // "3 + 4 = 7"

// 8. Add a linebreak tag -- <br> -- to the message variable

// 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"

// 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);

3 Answers

Julien Frenette
Julien Frenette
4,477 Points

Oh ok, I didn't look properly.

Ok so since the 'alert' seems to be written correctly, the problem is probably in your HTML. In the workspace, if you look on the left side, you should see that 'math.js' is nested inside the 'js' folder. This means the script tag should look like this:

<script src="js/math.js"></script>

If you already had it written like this, then I would suggest trying the 'console' in Chrome browser. If there is a mistake it should show up, with the line in question.

Julien Frenette
Julien Frenette
4,477 Points

You need to post your code so we can help you :)

Melissa Preece
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Melissa Preece
Full Stack JavaScript Techdegree Graduate 18,394 Points

Hi, not sure what you mean. I pasted the practice challenge and my code above. Is there a different way that I should do it? Apologies in advance, it's my first time reaching out for help here.

I just dragged out the math.js out of the js folder to make it a file on its own. Then typed in:

<script src="math.js"></script> and it worked for me.