Help with the cash register challenge? Can't get test 5 to pass

Hi,

I hope I’m in the right place. I’ve been working on this for I think a week, I’ve studied other peoples’ answers, and I’ve watched people live-coding a solution, but something is just not clicking for me for this challenge and I have no idea where my own solution is going wrong. I’m pretty sure I understand the concept of what I’m supposed to do, but my code seems very fragile, and I’m not sure why. Can anyone lend me a hand and point out where I’m going wrong? If I can just get all the tests to pass I’d be so happy.

Here’s where I’m at (minus all my copious console logging and comments):

function checkCashRegister(price, cash, cid) {
	let changeOwed = cash - price;
	let change = [];
	let total = 0;
	for (let changeOwed of cid) {
		total += changeOwed[1];
	}
	let registerContains = total.toFixed(2);
	let cashRegister = {
		status: '',
		changeOwed: []
	}
	const currency = {
		"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
	}
	for (let i = cid.length - 1; i >= 0; i--) {
		const currencyName = cid[i][0];
		const currencyTotal = cid[i][1];
		const currencyValue = currency[currencyName];
		let currencyAmt = (currencyTotal / currencyValue).toFixed(2);
		var changeReturn = 0;

		while (changeOwed >= currencyValue && currencyAmt > 0) {
			changeOwed -= currencyValue;
			registerContains -= currencyValue;
			changeOwed = changeOwed.toFixed(2);
			currencyAmt--;
			changeReturn++;
		}
		if (changeReturn > 0) {
			change.push([currencyName, changeReturn * currencyValue])
		}
	}
	if (changeOwed > 0) {
		cashRegister = {
			status: "INSUFFICIENT_FUNDS",
			change: []
		}
		return cashRegister;
	} else if ((changeOwed < 0) || (registerContains != 0)) {
		cashRegister = {
			status: "OPEN",
			change: [...change]
		}
		return cashRegister;
	} else {
		cashRegister = {
			status: "CLOSED",
			change: [...change]
		}
		return cashRegister;
	}
}

Add the following on the line before the if (changeOwed > 0) { line and you will see with the else if evaluates to true.

console.log(changedOwed);

Hi Randell,

I’m sorry, I don’t understand, I’m getting tired, haha. What should I add before that line? Thanks :slight_smile:

So sorry about that. I edited my post above.

Thank you for your help! I’ve been playing around with it a lot, however, I’m really just not getting it. I see how before I was saying if it was either more than zero or less than zero, so the final else could never get called. But I still can’t seem to get it working. Could I have another push in the right direction? I honestly can’t tell if I’m close to the answer or extremely far away. I’ve changed my code to the following…

function checkCashRegister(price, cash, cid) {

	let changeOwed = cash - price;
	let change = [];
	let total = 0;
	for (let changeOwed of cid) {
		total += changeOwed[1];
	}
	let registerContains = total.toFixed(2);
	let cashRegister = {
		status: '',
		changeOwed: []
	}
	const currency = {
		"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
	}
	for (let i = cid.length - 1; i >= 0; i--) {
		const currencyName = cid[i][0];
		const currencyTotal = cid[i][1];
		const currencyValue = currency[currencyName];
		let currencyAmt = (currencyTotal / currencyValue).toFixed(2);
		var changeReturn = 0;

		while (changeOwed >= currencyValue && currencyAmt > 0) {
			changeOwed -= currencyValue;
			registerContains -= currencyValue;
			changeOwed = changeOwed.toFixed(2);
			currencyAmt--;
			changeReturn++;
		}
		if (changeReturn > 0) {
			change.push([currencyName, changeReturn * currencyValue])
		}
	}
	console.log("The change owed is: " + changeOwed);
	if (changeOwed > 0) {
		cashRegister = {
			status: "INSUFFICIENT_FUNDS",
			change: []
		}
		return cashRegister;
	} else if ((changeOwed == 0) && (registerContains > 0)) {
		cashRegister = {
			status: "OPEN",
			change: [...change]
		}
		return cashRegister;
	} else {
		cashRegister = {
			status: "CLOSED",
			change: [...change]
		}
		return cashRegister;
	}
}

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

I have been messing with this a lot, and sadly, I’m still no closer. Am I way off here? I’d be very grateful if anyone has any ideas for what I could do. Thank you all, you’re wonderful. :slight_smile:

@Glitterbug You are not following the one instruction.

Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.

It tells you exactly what to assign to the change key/property. What is cash-in-drawer ? Do you have that available somewhere already?

Last Hint: You need to make a very small change to the following line in your else code block.

change: [...change]

And FYI - you only need a single return statement after your if/else if /else statements to return cashRegister. Otherwise, it is just repeated code. In fact, you can replace the following:

	let cashRegister = {
		status: '',
		changeOwed: []
	}

with:

	let cashRegister;

Thank you so much, I finally got it! After all this time, that was such a silly little thing, haha. I really appreciate all your help!