Cash Register Project

I have been able to get my code to pass all the tests on the JavaScript Algorithms and Data Structures Projects: Cash Register project except for the 5th one, where there is enough money to make change, but not the right kind of coins, so the change cannot be made.

I am getting the right result for this test: status: INSUFFICIENT FUNDS, change: [] and even the web console outputs the right result, but I can’t seem to get the test to pass. Any suggestions?

Here is my code:

function checkCashRegister(price, cash, cid) {

	var money = [["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.1], ["QUARTER", 0.25], ["ONE", 1], ["FIVE", 5], ["TEN", 10], ["TWENTY", 20], ["ONE HUNDRED", 100]];
	
	var cidTotal = 0;
	
	var changeAmt = cash - price;
	var changeArray = [];
	
	var numCoins = 0;
	var tempAmt;
	
	var transaction = {};
	
	function makeTransaction(status, change) {
		return {
			status: status,
			change: change
		};
	}
	
	
	// Calculate amount of money in cash register
	for (let i = 0; i < cid.length; i++) {
		cidTotal += cid[i][1];
	}
	cidTotal = Math.round(cidTotal*100)/100;
	
	// print out
	console.log("cid: " + cid);
	console.log("Cash in Drawer: " + cidTotal);
	console.log("Change Amount: " + changeAmt);
	
	// Calculate the status and the change
	if (changeAmt == cidTotal) {  // the change uses up all the cash in drawer
		
		transaction = makeTransaction("CLOSED", cid);
		
	} else if (changeAmt > cidTotal) { // there isn't enough cash in drawer to make change
		
		transaction = makeTransaction("INSUFFICIENT_FUNDS", []);
		
	} else {	// Make change with cash in drawer  *****************
	
		for (let i = cid.length-1; i >= 0; i--) {
			
			if (cid[i][1] != 0) {
				
				if (cid[i][1] <= changeAmt) {
					if (i == 0) {
						transaction = makeTransaction("INSUFFICIENT FUNDS", changeArray);
						break;
					}
					changeArray.unshift(cid[i]);
					changeAmt = Math.round((changeAmt - changeArray[0][1])*100)/100;
				} else {
					numCoins = Math.floor(changeAmt/(money[i][1]));
					
					if (numCoins != 0) {
						tempAmt = Math.round((numCoins*money[i][1])*100)/100;
						changeArray.unshift([cid[i][0], tempAmt]);	
						changeAmt = Math.round((changeAmt - changeArray[0][1])*100)/100;						
					}				
				}	
				
			}			
			
			if (changeAmt <= 0) {
				break;
			}
			
		}		
		
		if (changeArray[0] == undefined) {
			transaction = makeTransaction("INSUFFICIENT FUNDS", []);
		} else {
			changeArray.reverse();
			transaction = makeTransaction("OPEN", changeArray);
		}
		
		
	}
	
	console.log("Transaction: ");
	console.log(transaction);
	
	console.log("************************");
	
	return transaction;
}

You are going to kick yourself for this one. Look carefully at the instructions to see what the string value for the status property should be when there are insufficient funds and compare it with what you are returning.

Okay, I am really, really kicking myself! I spent hours trying to figure out why the test wasn’t passing. Thank you for noticing. :blush: