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 JavaScript Basics (Retired) Working With Numbers Using Math Methods

I'm trying alert(Math.floor(temperature)); - is this not correct? I'm getting "Bummer: Did you use Math.round()?

I'm getting an error when I try and submit this but when I hit preview I get an alert with 37 as expected.

script.js
var temperature = 37.5;
alert(Math.floor(temperature));
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

3 Answers

Steven Parker
Steven Parker
231,269 Points

You're expecting 37 because you're using "floor". But the instructions say "rounded to the nearest integer". So 38 would actually be the correct answer, which is what you'd get using "round".

Your first method choice might be more appropriate for task 2. :wink:

This is for task two. β€œRounded downward to the nearest integer” would be 37, right?

Steven Parker
Steven Parker
231,269 Points

Downwards would be 37, which is correct for task 2; but you must also observe the other instruction "Important: In each task of this code challenge, the code you write should be added to the code from the previous task."

So the task 1 alert code should remain as-is while you add the new one after it.

That was it, I didn't realize I HAD to add it afterward. I did so and it worked. Thanks.

Hi Elizabeth,

in this challenge you are asked to use Math.round and not Math.floor.

The difference is: Math.floor always rounds down ( 3.3 -> 3 ; 3.5 -> 3 ; 3.7 -> 3) while Math.round rounds to the nearest integer (3.3 -> 3 ; 3.5 -> 4).

So, with 37.5 the methods produce different results.

Makes sense?

Happy coding!

Nils

PS: You can upvote my post and/or mark as "best answer" (at the bottom of my post) if it helped you. :-)

For challenge two you are specifically asked to use the floor, which is what I'm having trouble with. "Open an alert dialog a second time and display the temperature variable rounded downward to the nearest integer. You'll need to check the Mozilla Developer Network to find the proper Math method for this (hint: down is toward the "floor".)"

Yes, for the second one it should be Math.floor.

part 1 answer alert(Math.round(temperature));

part 2 answer var temperature = 37.5; alert(Math.round(temperature)); alert(Math.floor(temperature));

Steven Parker
Steven Parker
231,269 Points

My understanding is that Treehouse discourages posting explicit answers without explanations. I like to think most students actually prefer a few hints and solving for themselves anyway.