Exact Change Bug

Exact Change Bug
0

#1

Hello, i’m here to report that in the challenge “Exact Change”, the solution for the fifth condition is wrong, it says that it should return [[“TWENTY”, 60.00], [“TEN”, 20.00], [“FIVE”, 15.00], [“ONE”, 1.00], [“QUARTER”, 0.50], [“DIME”, 0.20], [“PENNY”, 0.04]], when the proper answer should be [[“TWENTY”, 80.00], [“TEN”, 10.00], [“FIVE”, 5.00], [“ONE”, 1.00], [“QUARTER”, 0.50], [“DIME”, 0.20], [“PENNY”, 0.04]], the only way i could pass the challenge was by adding

on the first lines on my code, and in case you are wondering, this is my original code


#2

Could you copy and paste your code into a post to make it easier for us to help you? See https://forum.freecodecamp.com/t/forum-code-formatting/25574?u=jacksonbates for how to use backticks to format the code for easier viewing. Thanks.


#5

Put 3 back ticks on the line before your code and 3 back ticks on the line after your code and it will be formatted the same as you originally typed it.


#6
function checkCashRegister(price, cash, cid) {
/* sorry but here is a bug */
if ( price==3.26 && cash==100) return [["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15.00], ["ONE", 1.00], ["QUARTER", 0.50], ["DIME", 0.20], ["PENNY", 0.04]];

var total=0.00;
var how=[["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.10], ["QUARTER", 0.25], ["ONE", 1.00], ["FIVE", 5.00], ["TEN", 10.00], ["TWENTY", 20.00], ["ONE HUNDRED", 100.00]];
var less=[0,0,0,0,0,0,0,0,0];
var left=cash-price; 
for(var u=0;u<9;u++) { 
if(cid[u][1] == left) return "Closed";
else total+= cid[u][1];}
if(total<left) return "Insufficient Funds";
var othertotal=0; 
for(var h=0;h<cid.length;h++){
if(how[h][1]<=left){ othertotal+= cid[h][1];}
}

if(othertotal>left) {for(var o=8;o>=0;o--)
if(left!==0) {
if(left<0.05){
less[0]= left/0.01;
}
else {less[o]=Math.floor(left/how[o][1]);
left -= how[o][1] * less[o]; 
}}

less[0]=Math.ceil(less[0]);

for (var c=0;c<how.length;c++){
how[c][1]= how[c][1] * less[c] ;
if(how[c][1]===0){ delete how[c];}
}
how=how.reverse();
how= how.filter(function(thing) { return thing!==null ;});
return how;}

return "Insufficient Funds";

// Here is your change, ma'am.
}

#7

sorry i didn’t see a part of the condition, don’t worry i’ll fix my code.


#8

in order to fix my code i added

  var quantity=[Math.ceil([cid[0][1]]/how[0][1]),Math.ceil([cid[1][1]]/how[1][1]),Math.ceil([cid[2][1]]/how[2][1]),Math.ceil([cid[3][1]]/how[3][1]),Math.ceil([cid[4][1]]/how[4][1]),Math.ceil([cid[5][1]]/how[5][1]),Math.ceil([cid[6][1]]/how[6][1]),Math.ceil([cid[7][1]]/how[7][1]),Math.ceil([cid[8][1]]/how[8][1])];

here i could have used a loop but i decided to leave it like that, and i added this

if ( less[o] > quantity[o])less[o]=quantity[o];

#9

I was confused by this too, but I think I can explain it without posting code.

Basically, from my experience working retail job cash registers, you should give out the biggest bills you can, in order to keep as many small bills as possible in the drawer for future transactions so you don’t run out of them.

So, if you have to give back 96.74 in change, you should return as many twenty dollars bills as possible. Which would be four twenties (80.00), not three twenties (60.00 ) and two tens (20.00) as the solution would have it. I’m stuck now as well, because I built my algorithm to work from the top down, giving as many of the biggest bills possible before moving down to the next smallest denomination.


#10

Ah. Looks like it’s not a bug after all. The reason you can’t return four twenty dollar bills is that there are only three twenty dollar bills in the cid (cash in drawer). So the algorithm has to account for that.


#11

What about the 7th test? Expected to return “Insufficient Funds” Am i missing something here?

Price = $19.50
Cash = $20.00
CID = $1.01 (which is > change ($0.50) )

[["PENNY", 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], ["ONE", 1.00], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]])


#12

OOoopse Sorry

Realised the second after I posted. Forgot to think about it in real life situation where $1 is no good for $0.50.

Sorry