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 trialMarcos Melonee
Courses Plus Student 2,338 PointsMath.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".)
8 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsAssuming 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
Treehouse TeacherMath.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
2,100 Pointsvar temperature = 37.5; alert (Math.floor (temperature));
gives me a an error of did not paas 1 what am I doing wrong?
Bo Yuan
20,928 Pointsvar temperature = 37.5
alert(Math.round(temperature));
Li Chang
11,883 Pointsvar temperature = 37.5
alert(Math.round(temperature));
A X
12,842 PointsI 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
8,156 PointsThank you! This helped me with the second question.
Marcos Melonee
Courses Plus Student 2,338 Pointsohhh! thank's Jonathan!
Jonathan Grieve
Treehouse Moderator 91,253 PointsYou're welcome. :-)
Wouter Kirstein
2,100 PointsThanks! I managed to find it
Dave McFarland
Treehouse TeacherWouter 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.
Dave McFarland
Treehouse TeacherDave McFarland
Treehouse TeacherMarcos,
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