redwolf10105 redwolf10105 - 1 month ago 5
Javascript Question

Rounding makes program not function

I am creating a program that measures recycling success. It averages to find the answer. This has been making it difficult owing to the fact that 23.456323 doesn't really fit too well in the result box. I put in

math.round()
to round it. When I do this, it stops averaging. Here is my code:

$('.num').text(prompt('How is recycling? 1-100!'));

function colors() {
numb = Number($('.num').text());
if (numb < 30) {
$('.num').css('color', '#ff0000');
} else if (numb > 29 && numb < 79) {
$('.num').css('color', '#ffff00');
} else if (numb > 78) {
$('.num').css('color', '#00ff00');
} else {
$('.num').css('color', '#000000');
}
}
colors();
$('button').click(function() {
var how = Number(prompt('How is recycle? Rate it 0-100!'));
var calc = (Number($('.num').text()) + how) / 2;
$('.num').text(calc);
colors();
});


Here is the line that I put the rounding in, with and without the rounding:

$('.num').text(calc);
: Without rounding;

$('.num').text(math.round(calc));
: with rounding.

Answer

JavaScript is case-sensitive; as @Teemu pointed out in the comments, math does not reference the same thing as Math.

Your code references math which has not been defined in your code. Because math is undefined, you should be seeing a error message along the lines of ReferenceError: math is not defined in your browser console.

To fix it change math to Math to reference the Math object, which is implicitly defined as it is part of the language.

Comments