Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
218,833 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
218,833 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
218,833 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.