-2
    var time_gone = 0

    function timer() {
        time_gone += 0.01 
        console.clear()
        console.log(time_gone)
        setTimeout("timer()", 10)
    }

    timer()

Try using this code and look in the console

Why does the counter fail?!?!?!?

12321
  • 5
  • 2

1 Answers1

1

This is probably going to get closed as a duplicate, because that's just how JavaScript math works. Actually, most programming languages deal with floating-point numbers like that, so I suggest you really read the post linked above, and try to understand it.

Now, to fix the display of the numbers, you can use toFixed:

var time_gone = 0;
function timer() {
    time_gone += 0.01;
    console.clear();
    console.log(time_gone.toFixed(2));
    setTimeout(timer, 10);
}
timer();
Community
  • 1
  • 1
bfavaretto
  • 71,580
  • 16
  • 111
  • 150