Cash Register - stuck on a last test

EDIT: added notes to the code

EDIT 2 : I MADE IT WORK!! cid needed reversing so it returns an array in the same oreder as requested in teh test

So, after literally hours trying to solve that issue I am finally turning for some help.

I am stuck on a last test. Whatever what I do, it doesn’t seem to pass. I’ve tried in the console and the result seems ok.

Would anyone be able to help out?

Many thanks,

Link to the challange here

function checkCashRegister(price, cash, cid) {

	// result here
	var final = { status: "CLOSED", change: [] }
	var finalChange = [];
	//change array will hold values of the iteration and will be returned in a result
	var change = [["ONE HUNDRED", 0], ["TWENTY", 0], ["TEN", 0], ["FIVE", 0], ["ONE", 0], ["QUARTER", 0], ["DIME", 0], ["NICKEL", 0], ["PENNY", 0]];
	//notValue, holds numerical value for each note
	var noteValue = [["ONE HUNDRED", 100], ["TWENTY", 20], ["TEN", 10], ["FIVE", 5], ["ONE", 1], ["QUARTER", 0.25], ["DIME", 0.10], ["NICKEL", 0.05], ["PENNY", 0.01]];

	// works out the numerical value of a change to be returned to the customer
	var cashOwed = cash - price; 
	//totalCashInDrawer - returns total of numerical values in cid ( cash in drawer ) 
	var totalCashInDrawer = Number(cid.flat().filter(item => !isNaN(item)).reduce((a,b)=>a+b).toFixed(2)); 
	//reverse cid array so it matches change and noteValue variables pattern
	var reversedCid = cid.reverse()

	//Return {status: "CLOSED", change: [...]}with cash-in-drawer as the value for the key changeif it is equal to the change due.
	if(totalCashInDrawer == cashOwed) {
// reverse added here!! 
		final.change = cid.reverse() ; 
		return final;
	// Return {status: "INSUFFICIENT_FUNDS", change: []}if cash-in-drawer is less than the change due, or if you cannot return the exact change.
	if(totalCashInDrawer < cashOwed) {
		final.status = "INSUFFICIENT_FUNDS";
		return final;
	//iterate through cash in drawer -(reversedCid) and denominte its value accordning to the value of the note taken (noteValue), finally add that value to change variable - do for each element n the arr
   	if(totalCashInDrawer > cashOwed) {
	for(var i = 0; i < reversedCid.length; i++) {
		while((cashOwed >= noteValue[i][1]) && (reversedCid[i][1] >= noteValue[i][1])) {
			cashOwed -= noteValue[i][1];
			reversedCid[i][1] -= noteValue[i][1];
			change[i][1] += noteValue[i][1];
			cashOwed = cashOwed.toFixed(2)
	//push the result of the iteration above into the finalChange array	
		if(change[i][1] > 0) {
	//when cashOwed drops down to zero,  return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order, as the value of the changekey.
		if (cashOwed == 0) {
			final.status="OPEN", final.change=finalChange;
			return final;
	//in case there's not enough coins of needed value to return
		} else {
			final.status="INSUFFICIENT_FUNDS", final.change=[];
			return final;

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’m a little confused for your cashOwed == 0 seems to me that when the cashOwed hits 0 for the proper instance, you change the status to open. Don’t you need it closed?

Also, it’s really odd to me, but the final amount of money in the cashier is the same as it was before when the change equals that of the cashier.

Hope this helps.

So, the loop above " if (cashOwed == 0) " works out the exact change to be given. I tried following the logic set in the tast. When the number hits zero, it means that the program worked out what needs to be returned and it “opens” teh drawer. I’m guesing that part is right as tests around it are passed. It’s only the last test that I can’t pass:

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])should return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}

When tested in the console, I do get the right result thought.

I will add the comments to my code later today. You’re right, looking at it with fresh eyes does make it look confusing.