JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Trying to do this last challenge and I noticed a weirdity. Can someone explain why when javascript adds together 1.01 and 2.05 it equals 3.0599999999999996?
I checked in another javascript playground and the same thing happens. Is it something wrong with the actual JS code?

Your code so far

function checkCashRegister(price, cash, cid) {
 
    console.log(1.01 + 2.05)
}

checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

JavaScript (and nearly every other programming language) can’t exactly represent decimal numbers. I’d Google around about ‘floating point rounding’.

thanks. that will help me solve it, but I still would like to know why this is happening? I’ve never run into this with Java or Kotlin

That is how decimal numbers behave in Java and Koltin too. Like I said, I would start by Googling that phrase - the explanation is technical but there are some really good YouTube videos on it.

I think I’m beginning to see. It has to do with the actual binary code and representing decimals with binary is…weird

1 Like

Totally. Its a bit mind bending!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.