So closeeeeee please help

Tell us what’s happening:
I’m one step away from my first certification, but somehow this register refuses to work, the console outputs
{ status: ‘OPEN’,
change:
[ [ ‘TWENTY’, 60 ],
[ ‘TEN’, 20 ],
[ ‘FIVE’, 15 ],
[ ‘ONE’, 1 ],
[ ‘QUARTER’, 0.5 ],
[ ‘DIME’, 0.20000000000000018 ],
[ ‘PENNY’, 0.030000000000000027 ] ] }
for the call at the end, while it should be 0.2 and 0.04.
I know that this code is VERY messy but please help, organise the code on your own, this is a very delicate and small mistake that i completley don’t understand so any experienced users’ help will be appreciated.
Thanks!

Your code so far


function checkCashRegister(price, cash, cid) {
var totalCash = 0;
function totalCashCalc(arr) {
for (let i = 0; i < arr.length; i++) {
totalCash += arr[i][1]}}
totalCashCalc(cid);
var result = [];
var vals = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
var change = cash - price;

if (totalCash < change) {
return {status: "INSUFFICIENT_FUNDS", 'change': []}}
else if (totalCash == change) {
return {status: "CLOSED", 'change': cid}}
else {
for (let i = cid.length - 1; i >= 0; i--) {
var changeBefore = cid[i][1]; 
while (change >= vals[i] && cid[i][1] >= vals[i]) {
change -= vals[i]
cid[i][1] -= vals[i]}; 
result.push([cid[i][0], changeBefore - cid[i][1]])
}return {status: "OPEN", change: result.filter(function (a) {if (!a[1]) {return false}
else {return true}})} 
}
}
console.log(checkCashRegister(3.26, 100, [["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 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

should i hardcode with math.floor() and math.ceil()? i fill like there’ll be some other errors, and hardcodinng isn’t exactly the best finish for my certification project

Floating point numbers always have round off. It’s recommended that you use an integer number of cents for this project.

1 Like

I don’t understand, what should i do in this case then? Im just a casual noob compared to you

Right now you are using a floating point (decimal) number of dollars. Floating point numbers always accumulate error, like the .000000000000000018 you’re seeing.

Instead, it is recommended that you use an integer (whole number) of cents. Then you will always have the correct number of cents. Change in dollars is then given by dollars = cents / 100.

1 Like

from what i understood from other sites, your’e supposed to convert from ‘correct’ decimals to the base 10 numbers we see, i have no idea about this, should i just copy paste the solution from the ‘hints’? :confused:

And in that link, there is an example of using a number with and without decimals. Don’t use decimals for your numbers in this challenge.

i don’t, that’s what the tests give me

do i need to define a special unit for every decimal (pennies, quarters etc) and then divide it to dollars?

These are decimals with reference to the number of dollars. You are getting round off errors because you are doing arithmetic with decimals. You want to instead do arithmetic on an integer number of cents.

1 Like

so multiply everything by 100 and then at the end divide it? Thanks

1 Like