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

if alert(Math.floor(temperature)); is NaN then how do i get around this

just trying to get around this problem

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

1 Answer

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

I believe you are assigning a string to the temperature variable when you use alert to store the 37.5. So instead of saying:

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

Try removing the alert and just store the following for the temperature variable:

var temperature = Math.round(37.5);

Then the rest of the code should be fine once you have ammended this part.

I found by removing the Math.round() method from the temperature variable and doing this below actually functions correctly.

var temperature = 37.5;
alert(Math.round(temperature)); // expected output: 38
alert(Math.floor(temperature)); // expected output: 37

Based off your code, I went with this:

var temperature = alert(Math.round(37.5)); // expected output: 38
alert(Math.floor(temperature)); // expected output: NaN

But line 1 displays 38 and line 2 displays NaN because alert(Math.round(37.5)) is not a number. Am I interpretting this correctly?