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

Marcos Melonee
PLUS
Marcos Melonee
Courses Plus Student 2,338 Points

Math.round, I am not able to deal

http://teamtreehouse.com/library/javascript-basics-2/working-with-numbers/using-math-methods

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

so far so good then I do not understand what to do .. that's the question: Open an alert dialog a second time and display the temperature variable rounded downward to the nearest integer (hint: down is toward the "floor".)

Dave McFarland
Dave McFarland
Treehouse Teacher

Marcos,

To put code into a forum post use triple back ticks -- ``` β€” around the code. I fixed your code here, but in the future here's a forum discussion that describes how to add HTML, CSS, JavaScript or other code to the forum: https://teamtreehouse.com/forum/posting-code-to-the-forum

8 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Assuming that Math.round() rounds a number up, then as you've suggested, Math.floor() rounds a number down.

so...

var temperature = 37.5;

alert(Math.floor(temperature);

or

alert(Math.floor(37.5);

as you put it, would round the number down giving 37. :-)

Dave McFarland
Dave McFarland
Treehouse Teacher

Math.round() rounds to the nearest integer -- either up or down -- so Math.round(10.75) will result in 11 while Math.round(10.2) will produce 10.

Math.ceil() always rounds up to the nearest integer so Math.round(10.1) would result in 11.

Math.floor() always rounds down so Math.floor(10.9) will produce 10.

Wouter Kirstein
Wouter Kirstein
2,100 Points

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

gives me a an error of did not paas 1 what am I doing wrong?

Bo Yuan
Bo Yuan
20,928 Points
var temperature = 37.5
alert(Math.round(temperature));
Li Chang
Li Chang
11,883 Points
var temperature = 37.5
alert(Math.round(temperature));

I also wanted to explain the answer that Li Chang gave more in detail:

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

The reason why we use the variable temperature vs. the number itself is then we're not using the variable temperature like the directions state: "Open an alert dialog and display the temperature variable rounded to the nearest integer." We're using the number 37.5, but remember that programming languages are inherently "dumb" in that they can't infer. We can infer in this small code that 37.5 must relate to temperature's variable, but the computer cannot.

A real life example might be that you buy 2 tacos. I ask you to get me my taco (since I like extra cheese and chorizo), but you bring me your taco by accident since they both have the same wrapping paper. Yes they're both tacos, but they're also different from each other as your taco variable (extra veggies) is different from my taco variable (extra cheese and chorizo), even though they're both tacos...and I bet we'd both be super disappointed not getting the taco variable (var) that was declared to be the one we both wanted.

Drew Stefani
Drew Stefani
8,156 Points

Thank you! This helped me with the second question.

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Wouter Kirstein There are 2 tasks in the code challenge. The first task does not use the Math.floor() method -- is that where you are seeing the error? If so, look at the error message that appears -- it gives you a hint about what you need to do to complete that task.