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

temperature=Math.floor(temperature); alert(temperature); is this not the answer guys?

i cant seem to understand this . i am rounding an integer 38 downwards to the floor . temperature holds the interger 38 . Wont this be the way to round it down and alert it?

temperature=Math.floor(temperature); alert(temperature);

script.js
var temperature = 37.5;
temperature=Math.round(temperature);
alert(temperature);
temperature=Math.floor(temperature);
alert(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>

4 Answers

Hello Ashton,

You can't store the result of using Math.round or Math.floor because if you do, you change the original value of temperature. temperature becomes 38 after the first conversion. And then running Math.floor on it will keep it at 38, when that shouldn't be the case.

You only need to alert each Math method being used on the variable temperature

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

thank you!! i understand now

Tony Luo
Tony Luo
14,224 Points

What exactly are you trying to do here?

after getting the right code here,..

var temperature = 37.5; temperature=Math.round(temperature); alert(temperature);

which the answer was 38 . they told me to

Open an alert dialog a second time and display the temperature variable rounded downward to the nearest integer (hint: down is toward the "floor".)

Hugo Paz
Hugo Paz
15,622 Points

Hi Ashton,

You start with the temperature value of 37.5

You then round its value, temperature = Math.round(temperature);

So now temperature = 38.

Then you round it down to the closest integer with Math.floor(temperature).

The closest integer to 38 is 38 this is why you see 38.

If you dont round the value first, with Math.round(temperature), temperature is set to 37.5.

Rounding it down with floor looks for the closest integer smaller than the value itself. So you have 37.5, the closest smaller integer is 37.