Cash Register 1 test wont pass

Tell us what’s happening:

hi there! Im having a problem passing one of the provided test cases, even though the console.log() shows, what seems to be, the exact return object as shown in the FCC examples.
test input: 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]])

expected output: {status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}

my console output: {status: “OPEN”, change: Array[7]->7xArray[2]]}, then when I look at the values inside the inner array they seem to be the same as in the expected output…

I googled and googled and all I could find were people having issues after forgetting to round up the change.

Your code so far


var moniez = [
	{ denom: "PENNY", 	value: 0.01 },
	{ denom: "NICKEL", 	value: 0.05 },
	{ denom: "DIME", 	value: 0.10 },
	{ denom: "QUARTER", value: 0.25 },
	{ denom: "ONE",		value: 1.00 },
	{ denom: "FIVE", 	value: 5.00 },
	{ denom: "TEN", 	value: 10.00 },
	{ denom: "TWENTY", 	value: 20.00 },
	{ denom: "ONE HUNDRED", value: 100.00 }
];

function checkCashRegister(price, cash, cid) {
  var change = cash - price;
  // total cash in the drawer
  var cid_tot = Number(cid.reduce((a, c) => { a += c[1]; return a }, 0.0).toFixed(2));
  if (cid_tot < change) {
  	return {status: "INSUFFICIENT_FUNDS", change: []};
  }
  else if (cid_tot === change) {
  	return {status: "CLOSED", change: cid};
  }
  
  cid = cid.reverse();
  moniez = moniez.reverse();
  var ans = moniez.reduce((agg, curr, idx) => {
  	if (change >= curr.value) {
  		let tot_paid = 0;
  		while (change >= curr.value && cid[idx][1] > 0) {
  			tot_paid += curr.value;
  			change -= curr.value;
  			cid[idx][1] -= curr.value;
  			change = Math.round(change * 100)/100;
  		}
  		agg.push([curr.denom, tot_paid]);
  		return agg;
  	}
  	else {
  		return agg;
  	}
  }, []);

  return ans.length > 0 && change === 0 ? {status: "OPEN", change: ans} : {status: "INSUFFICIENT_FUNDS", change: []};
}

// Example cash-in-drawer array:
// [["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 5.1; rv:52.0) Gecko/20100101 Firefox/52.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/

I believe your code is breaking the test in the wrong place than is being shown, because you’re doing floating point math.

Take a look at the screenshot below of a console statement using these arguments

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

The first result is the result of calling the function manually within your code. The second is the result received when your code is being tested. The answers should be the same, but they’re not. And the first is correct, while the second isn’t.

08

Somehow this is causing the next test to fail. If you comment out the function call inside your code, you’ll see this result.

I’d suggest not doing floating point math – which has weird bugs in any language, and converting all denominations into the lowest common denominator.

You could only do floating point math with specialized libraries that gracefully handle these type of edge cases.

I could also be wrong and it’s not a floating point math bug. But my instincts tell me that it will resolve your bug.

yup… when I console.log() the checkCashRegister function call directly in the Chrome DevTools it shows the second result, thanks!