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

Hello , I tried solving the challenge using a certain logic but the compiler shows me an error. It worked on JSFiddle.

Question - 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".)

Does this code solve the challenge too ? If its a yes then the editor on tree house is prompting me its the wrong answer but it worked fine on JSFiddle and codepen .. Please check and let me know If I misunderstood the question or if there is a technical problem which is not letting me pass the challenge .

Question - 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".)

Thanks in advance.

script.js
var temperature = 37.5;
var temp= Math.round(temperature);
alert("The rounded temperature is " + temp);
var downRounded= Math.round(-1 * temperature ); 
alert("The down rounded temperature is " + (-1*downRounded));
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>

2 Answers

Quick glance: The question says to round your integers. (hint: down is toward the "floor".)

console.log(Math.floor(37.95));
// expected output: 37  

Link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

Hope this helps,
Cc

Thank you, I didnt know there was a method called Math.floor() . Also, may I know if my code functions the same as yours ? or is there a functionality problem in mine ..

Thanks again.

key tip: in this field it is impossible to know everything, you need to know how to look it up.
By taking courses and classes you will learn about the existence of certain methods, but you do not need to know all of them by heart. If I need to use something for a specific case or if i'm unsure about the syntax or what it specifically did again, I look it up.

This is what I found on the web regarding the difference of 'floor' and 'ground':

Math.round() - If the decimal portion of number is 0.5 or greater, the return value is equal to the smallest integer greater than number. Otherwise, round returns the largest integer less than or equal to number.

Math.floor() - The return value is an integer value equal to the greatest integer less than or equal to its numeric argument.

Math.ceil() - The return value is an integer value equal to the smallest integer greater than or equal to its numeric argument.

Math.round(1.6) // 2 
Math.floor(1.6) //- 1 
Math.ceil(1.6) //- 2 

Math.round(1.4) // - 1  
Math.floor(1.4) //- 1  
Math.ceil(1.4) // - 2    

I hope this helps,
Cc