Cash Register Project not completing despite correct output

function checkCashRegister(price, cash, cid) {
	const value = [
		["PENNY", 0.01],
		["NICKEL", 0.05],
		["DIME", 0.1],
		["QUARTER", 0.25],
		["ONE", 1],
		["FIVE", 5],
		["TEN", 10],
		["TWENTY", 20],
		["ONE HUNDRED", 100],
	];

	//find sum of money in register
	let register = 0;
	for (let i = 0; i < cid.length; i++)
		register += cid[i][1];
	
	let balance = parseFloat(register.toFixed(2));

	let difference = cash - price;

	if (difference > balance) { return { status: "INSUFFICIENT_FUNDS", change: [] }; }
	if (difference == balance) { return { status: "CLOSED", change: cid }; }

	//create an advanced array with amount of coins/notes & value of each
	let advanced = [...cid];
	for (let i = 0; i < advanced.length; i++) {
		let amount = advanced[i].pop(); //enables insertion in the middle and calculations
		advanced[i].push(value[i][1]); //the value of that single coin/note
		let qty = Math.round(amount / value[i][1]); //round because of some stupid .99999999
		advanced[i].push(qty); //the quantity of coins/notes
		advanced[i].push(amount); //the value of all the notes/coins of this value (value * qty)
	}
	//I have kept the actual worth of a certain note at the end for ease of manipulation with .pop() & .push()

	let change = [];
	let inHand = 0;
	for (let i = 8; i >= 0; i--) {
		if (advanced[i][3] == 0) {
			continue;
		} //this bill is not present in the drawer

		if (difference > advanced[i][3]) {
			//take all the qty of a note
			let temp = [];
			difference -= advanced[i][3];
			balance -= advanced[i][3];
			inHand += advanced[i][3];
			temp.push(advanced[i][0]); //push name of bill to temp
			temp.push(advanced[i].pop()); //push worth of bill to temp
			advanced[i].pop(); //replace current worth ...
			advanced[i].push(0); // ... with 0
			change.push(temp);
			difference = +difference.toFixed(2);
			balance = +balance.toFixed(2);
			inHand = +inHand.toFixed(2);
		} else if (difference == advanced[i][3]) {
			//if difference matches the worth of the note
			let temp = [];
			temp.push(advanced[i][0]); //push name of bill to temp
			temp.push(advanced[i].pop()); //push worth of bill to temp
			advanced[i].pop(); //replace current worth ...
			advanced[i].push(0); // ... with 0
			change.push(temp);
			break;
		} else if (
			difference != advanced[i][3] &&
			difference >= advanced[i][1]
		) {
			//if difference < sum but > value of 1 note
			let temp = [];
			temp.push(advanced[i][0]); //name of bill/coin
			temp.push(advanced[i][1]); //value of bill/coin
			difference -= advanced[i][1];
			balance -= advanced[i][1];
			inHand += advanced[i][1];
			difference = +difference.toFixed(2);
			balance = +balance.toFixed(2);
			inHand = +inHand.toFixed(2);
			advanced[i].pop(); //remove current worth
			let tmpQty = advanced[i].pop(); //get current qty
			tmpQty--; //dec qty (remove one bill/coin)
			advanced[i].push(tmpQty); //append new qty
			tmpVal = tmpQty * advanced[i][1]; //calculate new worth (qty * value)
			advanced[i].push(tmpVal); //append new worth
			change.push(temp);
			i++;
		}
	}

	if (difference == 0) {
		//remove redundancies
		for (let i = 0; i < change.length; i++) {
			let reps = 0;
			for (let j = i + 1; j < change.length; j++) {
				if (change[i][0] == change[j][0]) {
					change[i][1] += change[j][1];
					change.splice(j, 1);
					i = 0;
				}
			}
		}

		if (balance == 0) { return { status: "CLOSED", change: change }; }
		else { return { status: "OPEN", change: change }; }
	}
	else { return { status: "INSUFFICIENT_FUNDS", change: [] }; }
}

I have 3 tests that my code hasn’t passed but my output is correct.

  • First:
    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]]) should return an object.
    this does return an object and I have verified it with the typeof operator.

  • Second:
    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]]) should return {status: "OPEN", change: [["QUARTER", 0.5]]}
    Again, my code returns exactly that.

  • Third:
    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 just that.

What am I missing here?
Thanks a lot.

Just checked,
Your code passed all the tests for me.
Must be something in your browser that is causing the issue.
have you disabled adblockers etc?
also try resetting your code and pasting it back in

Take a look at what’s printed out to console, when you try one of the failing cases.

For example, add at the bottom:

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

Just submitted it through Edge instead of Chrome and it worked! thanks a lot Damian!
ps. it also had a lil bug where I forgot to add let