redwolf10105 redwolf10105 - 10 months ago 38
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

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');
$('button').click(function() {
var how = Number(prompt('How is recycle? Rate it 0-100!'));
var calc = (Number($('.num').text()) + how) / 2;

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

: Without rounding;

: with rounding.

Answer Source

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.