Decimal Division is stopping my certification

Tell us what’s happening:
Describe your issue in detail here.

i wrote this code for the cash register challenge but this user story is not fulfilled because of decimal division

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]]) should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}

my code returns
{ status: ‘OPEN’,
change:
[ [ ‘TWENTY’, 60 ],
[ ‘TEN’, 20 ],
[ ‘FIVE’, 15 ],
[ ‘ONE’, 1 ],
[ ‘QUARTER’, 0.5 ],
[ ‘DIME’, 0.2 ],
[ ‘PENNY’, 0.03 ] ] }

Pls help!!!
Your code so far


function checkCashRegister(price, cash, cid) {
let a = cash - price;
let b = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
let c = cid.map(curr => Math.round(curr[1] / b[cid.indexOf(curr)])); //checking how many bills and coins are available
let d = b.filter(unit => Math.floor(a / unit) > 0); //filtering bills and coins for change
let e = d.reduce((pre,cur) => pre + cid[d.indexOf(cur)][1], 0); // sum of cid
//checking if cid is enough for change

if (a>e){
  return {status: "INSUFFICIENT_FUNDS", change: []};
}

if (a == e){
//console.log(cid);
return {status: "CLOSED", change: cid};
}

/* calculating the number of notes to give as change
I do not forget to check the available number of notes
*/
let changeBills = [];
let g = a;
for (let i = d.length - 1; i >= 0; i--){
let f = Math.floor(g / d[i]);
if (f <= c[i]){
  changeBills.unshift(f);
  g -= (f * d[i]).toFixed(2);
}
else {
  changeBills.unshift(c[i]);
  g -= (c[i] * d[i]).toFixed(2);
}
console.log(`g is ${g}`);
}

// let billNames = changeBills.map(hey => [cid[changeBills.indexOf(hey)][0], b[changeBills.indexOf(hey)] * hey ]); //this map returns erroneous values for hey values that occur more than once

let change = (cid.map(j => [j[0], changeBills[cid.indexOf(j)] * d[cid.indexOf(j)]])).slice(0,d.length).filter(k => (k[1] != 0));

console.log(c);

/* console.log(a is ${a});
console.log(b);
console.log(c);
console.log(d);
console.log(e);
console.log(changeBills);
console.log(billNames);
console.log(change);
*/

return {status: "OPEN", change: change.reverse()};


}

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]]);

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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36

Challenge: Cash Register

Link to the challenge:

.1+.2
0.30000000000000004 

That is the answer you will get in the console. When I did this project I went off on a tangent researching Number Epsilon . But I think it is as easy as multiplying by the number of decimal places you need to have in your result. For example there is 100 pennies in a dollar. If you covert all of your amounts to pennies (X100) that might work. That speculation on my part seems how I went the long route using Number Epsilon. Computers only know 1,0 .

1 Like

Personally, I like the easy route - toFixed that sucker (which converts it to a string of the given precision), and then convert it back to a number.

1 Like

Yeah, I’m really surprised they don’t mention the number division issues associated with javascript (and other languages) in the project page… it took me forever to figure out why I was getting something like 1 / 4 = .25000000000002. I guess its something you’ll come across in the future with other projects… seems the easiest solution is like previously mentioned, mulitpy out by 10s how many decimal places you want, round that number, then divide it back.

I believe I dabbled with toFixed with this project with no success. But, that’s because something else looked shiny. Probably was an order of operations error combined with what we call in CNC machining “Operator error”. It’s never the machine.

1 Like

No one said you had to use floats. Do it the COBOL-ish way and use integer cents.

3 Likes

Thanks

I multiplied by 100 and that resolved it

Thanks

I resolved it by multiplying by 100

Thanks

That’s what I did

Yeah, they should include a lesson on decimal division

The sucker refused toFixed :joy:

Thanks, I resolved it by multiplying by 100

1 Like

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