# Only one condition is not met

so all conditions to pass this are met except one, and I cant tell why at the moment. If the drawer has only 50 cents worth or pennies (1 cent) and nothing else, the while statements work perfectly by continuing to subtract from the 50 cent change until its zero and the return array says [[“pennies”, .50]], but if the drawer has more than just pennies and the customer needs change that is all kinds of denominations, no subtractions and additions take place according to the 9 while loops that start at the top of the page with the largest denomination (100) and move down checking to see if the change is greater than the next largest denomination. I will put the code in here if it will let me.

CODE IS BELOW.
ABOVE IS EXPLANATION OF PROBLEM
THANKS

``````function checkCashRegister(price, cash, cid) {

price = price * 100;
cash = cash * 100;

for (let u = 0; u < cid.length; u++) {
cid[u][1] = cid[u][1] * 100;
};

// multiplying the price, cash provided and cid by 100 to avoid non-decimal calculations //

var cH = cash - price;

console.log(cH)

var cHA = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];

var ind = [1, 5, 10, 25, 100, 500, 1000, 2000, 10000];

// line above has all denominations multiplied by 100 as to avoid subtraction and addition of decimal point numbers. The variable ind also acts as a reference, for example, if we have a few one hundreds in the drawer and the change required is above 100, we can say, subtract 10000 from the drawer (remembering that everything in the drawer was multiplied by 100 (or moved two decimal places forward), add 10000 to our array which has all nine denominations initailly at zero, and take 10000 from change (cH), then it can move forward by again asking if change is still above 10000 or not, if it isnt, the next 'while' loop will be checking to see if the change is now above the 20 dollar note and so on //

var drawTotal = 0;

//starting the while loops at the highest denomination. If the while loop is true, the empty array with all 9 denominations started at 0 will be added to accordingly, cid will be subtracted from accordingly and change will be subtracted from accordingly //

while (ind[8] <= cH && cid[8][1] >= cH) {
cHA[8][1] += ind[8];
cid[8][1] -= ind[8];
cH -= ind[8];
};

while (ind[7] <= cH && cid[7][1] >= cH) {
cHA[7][1] += ind[7];
cid[7][1] -= ind[7];
cH -= ind[7];
};

while (ind[6] <= cH && cid[6][1] >= cH) {
cHA[6][1] += ind[6];
cid[6][1] -= ind[6];
cH -= ind[6];
};

while (ind[5] <= cH && cid[5][1] >= cH) {
cHA[5][1] += ind[5];
cid[5][1] -= ind[5];
cH -= ind[5];
};

while (ind[4] <= cH && cid[4][1] >= cH) {
cHA[4][1] += ind[4];
cid[4][1] -= ind[4];
cH -= ind[4];
};

while (ind[3] <= cH && cid[3][1] >= cH) {
cHA[3][1] += ind[3];
cid[3][1] -= ind[3];
cH -= ind[3];
};

while (ind[2] <= cH && cid[2][1] >= cH) {
cHA[2][1] += ind[2];
cid[2][1] -= ind[2];
cH -= ind[2];
};

while (ind[1] <= cH && cid[1][1] >= cH) {
cHA[1][1] += ind[1];
cid[1][1] -= ind[1];
cH -= ind[1];
};

while (ind[0] <= cH && cid[0][1] >= cH) {
cHA[0][1] += ind[0];
cid[0][1] -= ind[0];
cH -= ind[0];
};

// ok, so the array (cHA) which initially holds all 9 denominations at value 0, should be either added to or not. The cid should show the 9 denominations with the values of some of them subtracted from or not, and the change (cH) should return either 0 or above, depending on whether the cid provided had enough total currency but lacked the currency to provide exact change, whether the cid provided had enough total currency and was able to provide exact change, or whether the cid provided didnt have enough total currency or the currency to provide exact change.//

console.log(cH)

for (let r = 0; r < cid.length; r++) {
drawTotal += cid[r][1];
};

// the above 'for loop' totals the money left in the till/cid/drawer so that if it equals exactly 0, the program can return "CLOSED" and the change needing to be given, or if it doesnt equal 0, the program can return "OPEN" and the change needing to be provided //

cH = cH / 100;

// line above brings the change back two decimal places as the change was originally amplfied by multiplication of 100 to avoid addition and subtraction of decimal point values //

for (let p = 0; p < cid.length; p++) {
cid[p][1] = cid[p][1] / 100;
cHA[p][1] = cHA[p][1] / 100;
}

// line above brings cid and cHA "back down to earth", likely into decimal point notation //

var rArr = [];

for (let e = 0; e < cHA.length; e++) {
if (cHA[e][1] !== 0) {
rArr.push(cHA[e]);
};
};

// for loop above pushes only non zero values of the complete 9 denomination change array into an array that starts completely empty (rArr) which was declared just above //

var retObj = {status: null, change: []};

if (cH !== 0.00) {
retObj.status = "INSUFFICIENT_FUNDS";
retObj.change = rArr;
return retObj;
} else if (cH === 0 && drawTotal === 0) {
retObj.status = "CLOSED";
retObj.change = cHA;
return retObj;
} else if (cH === 0 && drawTotal !== 0) {
retObj.status = "OPEN";
retObj.change = rArr;
return retObj;
};

};

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

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

``````
Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36`.

Challenge: Cash Register

I’m not quite sure where the error is, but it definitely looks like something to do with the while loops. The cH variable is not returning 0. I would suggest seeing if you can condense those 9 while loops into one while loop. You could make a copy of the cH variable into a remainder variable that you could keep subtracting the denomination values from as long as the while loop returns true. That might be a start, but it looks like you are pretty close. I will try looking at it more tomorrow.

Hope this helps!

Thanks for the reply. It’s funny because when I put in 50 (price) and 100 (cash), it works well and gives two 20s and a ten. But when I put 30 (price) and 100 (cash), it gives three 20s and ten 1s. I changed the 9 while loops into an IF followed by 8 ELSE IFs, and then put that into a FOR loop that runs 200 times just to see if it was an order problem, but no, same response. I don’t see any way of condensing into one while loop. Anyway, yes, that would be helpful.

Works now. I made a FOR loop that goes through variable ‘ind’, starting at highest denomination, and then a ‘while’ loop inside of it that will keep running if change (cH) is greater than or equal to the current ind array index AND cid’s quantity of that denomination is greater than or equal to the current change value (cH). If you look at my old code, you can see that the if statement is the same but I put that into a while statement, and that while statement is inside a for loop). Also, everytime that statement ran true, after all subtractions and additions tok place, the while loop then rounded everything as some calculations caused rounding errors. Ill put the part of the code I am talking about if it saved.

``````for (let c = 8; c >= 0; c--) {
while (ind[c] <= cH && cid[c][1] >= ind[c]) {
cHA[c][1] += ind[c];
cid[c][1] -= ind[c];
cH -= ind[c];
cHA[c][1] = Math.round(cHA[c][1] * 1e2) / 1e2;
cid[c][1] = Math.round(cid[c][1] * 1e2) / 1e2;
cH = Math.round(cH * 1e2) / 1e2;
}
};
``````
1 Like