# My first request here. No error and I am stuck. Please help. What am I missing here?

Hello everyone.

This is the first time I am asking about my code.

I started coding by myself, I am only studying at freecodecamp and nothing else.
I think I was doing good actually, until I came to a point that I need a real help from someone else, because I am stuck for some days at this project, and the anytime I said “now I got it”, it didn’t work.

My code doesn’t give an error, but it doesn’t meet some criteria of the solution, I don’t know why.

First of all, I am sorry for primitive coding style, I confess I am just a beginner and I don’t have anyone else to correct me. So please think about it when you harshly criticise my code. I know.

Anyway, after posting my code, I will try to explain what I have done.

my code is here (it is Cash Register Challenge):

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

var numPrice = price.toFixed(2);
var numCash = cash.toFixed(2);
var numCoin = [];
var revCoin = [];
var totalCid = [];
var totalCid100 = [];
var totalAmount = 0;
var coinUnits = [1, 5,10, 25, 100, 500, 1000, 2000, 10000];
var numChange = [];
var changeArr = [];
var cashBackArr= [];
var revCashBackArr= [];
var clearArr =[];
var pushedArr= [];
var tempArr=[];
var otherArr =[];
var combinedArr = [];
var nonZeroArr = [];
var diffArr = [];
var lastObj = {};
var unitArr = ["PENNY", "NICKEL","DIME","QUARTER","ONE","FIVE","TEN","TWENTY","ONE HUNDRED"];
var revUnitArr = [];

for (let i = 0; i < cid.length; i++) {
numCoin[i] = ((cid[i][1]*100).toFixed(2)/coinUnits[i]);
totalCid.push(cid[i][1]);
totalCid100.push((cid[i][1]*100).toFixed(2))
totalAmount = (totalCid.reduce((a, b) => a + b, 0).toFixed(2));
}

revCoin = numCoin.slice().reverse();
revUnitArr = unitArr.slice().reverse();
let change100 = Math.round((numCash*100).toFixed(2) - (numPrice*100).toFixed(2));
let total100 = Math.round((totalAmount*100).toFixed(2));

//first console
console.log("change100",change100, "total100", total100)
console.log("numCoin",numCoin)

function sortCoin(change, numCoin) {
var numChange = [];
for ( let i=0;i<numCoin.length;i++)
{
numChange[8] = (change - (change % 10000))/10000;
if (numChange[8]>numCoin[8]) { numChange[8]=numCoin[8]};
numChange[7] = ((change - numChange[8]*10000)-(change%2000))/2000;
if (numChange[7]>numCoin[7]) { numChange[7]=numCoin[7]};
numChange[6] = ((change - numChange[8]*10000 - numChange[7]*2000)-(change%1000))/1000;
if (numChange[6]>numCoin[6]) { numChange[6]=numCoin[6]};
numChange[5] = ((change - numChange[8]*10000 - numChange[7]*2000-numChange[6]*1000)-(change%500))/500;
if (numChange[5]>numCoin[5]) { numChange[5]=numCoin[5]};
numChange[4] = ((change - numChange[8]*10000 - numChange[7]*2000-numChange[6]*1000-numChange[5]*500)-(change%100))/100;
if (numChange[4]>numCoin[4]) { numChange[4]=numCoin[4]};
numChange[3] = ((change - numChange[8]*10000 - numChange[7]*2000-numChange[6]*1000-numChange[5]*500-numChange[4]*100)-(change%25))/25;
if (numChange[3]>numCoin[3]) { numChange[3]=numCoin[3]};
numChange[2] = Math.round(((change - numChange[8]*10000 - numChange[7]*2000-numChange[6]*1000-numChange[5]*500-numChange[4]*100-numChange[3]*25)-(change%10))/10);
if (numChange[2]>numCoin[2]) { numChange[2]=numCoin[2]};
numChange[1] = ((change - numChange[8]*10000 - numChange[7]*2000-numChange[6]*1000-numChange[5]*500-numChange[4]*100-numChange[3]*25-numChange[2]*10)-(change%5))/5;

numChange[0] = ((change - numChange[8]*10000 - numChange[7]*2000-numChange[6]*1000-numChange[5]*500-numChange[4]*100-numChange[3]*25-numChange[2]*10-numChange[1]*5)-(change%1))/1;
return numChange;
}
}
numChange = sortCoin(change100, numCoin);

//second console
console.log("numChange", numChange)

for (let a = 0; a<numChange.length; a++) {
if (numChange[a]>numCoin[a]) {
numChange[a]=numCoin[a]
};
}

cashBackArr = [["PENNY", numChange[0]*0.01], ["NICKEL", numChange[1]*0.05],["DIME", numChange[2]*0.10],["QUARTER", numChange[3]*0.25],["ONE", numChange[4]*1],["FIVE", numChange[5]*5],["TEN", numChange[6]*10],["TWENTY", numChange[7]*20],["ONE HUNDRED", numChange[8]*100]];

revCashBackArr = cashBackArr.slice();
revCashBackArr.reverse();
for (let k = 0; k < revCashBackArr.length;k++) {
if (revCashBackArr[k][1] !== 0) {
clearArr.push(revCashBackArr[k]);}
}
//third console
console.log ("clearArr", clearArr);

if(change100 > total100) {
return {status: "INSUFFICIENT_FUNDS", change: []};
}

if(change100 == total100) {
return {status: "CLOSED", change: [...cid]}
}

for (let i = 0; i < cashBackArr.length; i++) {
if (change100 < total100 && cashBackArr[1][i]>=cid[1][i]) {
return {status: "INSUFFICIENT_FUNDS", change: []};
}  else if (change100 < total100 && cashBackArr[1][i] < cid[1][i]){
return {status: "OPEN", change: [...clearArr]};
}

}
}

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

USer Agent I took:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0

Now, I am sharing what I have thought by these lines.

1. I know there may be a lot of unnecessary variables, I will try to refine them, delete unused ones. But for the learning purpose, I am trying to set every variables.

2. I have been hurt by the toFixed(2) and round() and variable*100 kind of conversions. I started with normal values, but later I realized I need to multiply everything by 100. And for example, in the 25 cents, I really had difficult times, but I solved.

3. I calculated total Amount of Money in cash-in-drawer. I calculated cash-price =change100

4. I am checking the results by using console.log() at most of the turning points.

5. I spent very most of my time creating a sortCoin function. It isa result of very delicate and elaborated work of hours and hours. and it works, I tried it one by one. I put “if cases” between the lines, as cash-in-drawer may have less amount of money.

6. normally, I deleted the for(a=0) part, as it is useless, but I tried to change the if lines in the function sortCoin(), so I left it here for now.

7. I created and array for CashBack.

8. I cleared the zero elements of the array.

9. I tried to make comparisons for each case.

10. if(change100 > total100) works. if(change100 == total100) is working too.

11. I am not sure if I made it right but

if (change100 < total100 && cashBackArr[1][i]>=cid[1][i])

meaning change is less than total amount, but the cash back is more than the current money in the drawer. status: “INSUFFICIENT_FUNDS”, change: , so it works I guess…

And the last part…

And I failed at the latest part I guess.

else if (change100 < total100 && cashBackArr[1][i] < cid[1][i]){
return {status: “OPEN”, change: […clearArr]};

meaning change is enough in drawer, and the cash back is less than the money in the drawer. clearArr is the array that zero elements are reduced.

I am in serious need of help. I am dealing with this project for many days, and I am stuck.

Please help me. What am I missing here? (except the high quality coding style)

well, declaring 20 array can be enough to catch some bugs try to refine your code remove unnecessary arrays
they say “declare variables not a war”

``````for (let i = 0; i < cashBackArr.length; i++) {
if (change100 < total100 && cashBackArr[1][i]>=cid[1][i]) {
return {status: "INSUFFICIENT_FUNDS", change: []};
}  else if (change100 < total100 && cashBackArr[1][i] < cid[1][i]){
return {status: "OPEN", change: [...clearArr]};
}
}
``````

Consider couple things here:

• Is it possible for `change100` to be not lower than `total100` at this point in code?
• What happens when ie. for `i = 0`, the `cashBackArr[1][0]` is lower than `cid[1][0]`, is that what should be happening?.
• Is there a condition that all change of specific denomination cannot be returned as a change, or that it cannot be composed of the lower denomination?

You can wrap the `checkCashRegister` calls at the end in `console.log` calls, to make sure what is returned by function.

I will first try to refine the code, remove unnecessary lines.
then I’ll come back here again

uhm, I constructed this expression because I wasn’t able to decide how to handle the situation of change= only 0,5 and cash in drawer= only 0,01 and 1.

I thought I could solve it like:
First of all, at least all kinds of coins and banknotes should exist in drawer so that cash back would be possible.

if any kind of coin is not there, or less than the cash, then status:insufficient,
so I was checking if cash 0.5 (which is cashBackArr[i][1]) is that we need is less than cash in drawer [i][1]

(here, after your message, I now saw I made mistake of typing [1][i] instead [i][1] and also putting >= instead of >)

so:

• change100 would be larger if there’s not enough money in drawer (but you are right, there is no situation like that in the challenge)

• if corrected, would checking with the logic “cashBackArr[i][1] > cid[i][1]” work?
in which if cashback [quarter], 2 is bigger than cash-in-drawer [quarter], 0

• If I understood your question right, I am trying to compose the cash into the most possible denomination, which i start from the biggest currency, but I have put an if case in order not to allow it to be composed of the lower denomination. So, I think, there is no condition like that, because I am not responsible for the cash, but I am responsible for arranging the cash to give back from the drawer.

I don’t know if I am thinking right.
I may also have let myself go needless deeper. For example, I didn’t see anybody that says “why are there no 50 CENTS here?”

I am now going to edit the code to refine it. Thanks for your comments.

YAY! Working

I have still like 10 variables. But this is because of floats, toFixed(2) issues, and due to the reason that I am content to see the reversed arrays as individuals.

Is 10 too many for that challenge?

Which parts can I improve in this one?

What can I do to improve myself??

thanks!

Summary

function checkCashRegister(price, cash, cid) {

var numCoin = ;

var totalCid = ;

var totalAmount = 0;

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

var numChange = ;

var cashBackArr= ;

var revCashBackArr= ;

var clearArr =;

var lastObj = {};

var unitArr = [“PENNY”, “NICKEL”,“DIME”,“QUARTER”,“ONE”,“FIVE”,“TEN”,“TWENTY”,“ONE HUNDRED”];

for (let i = 0; i < cid.length; i++) {

numCoin[i] = ((cid[i][1]*100).toFixed(2)/coinUnits[i]);

totalCid.push(cid[i][1]);

totalAmount = (totalCid.reduce((a, b) => a + b, 0).toFixed(2));

}

let change100 = Math.round((cash100).toFixed(2) - (price100).toFixed(2));

let total100 = Math.round((totalAmount*100).toFixed(2));

function sortCoin(change, numCoin) {

var numChange = ;

for ( let i=0;i<numCoin.length;i++) {

numChange[8] = (change - (change % 10000))/10000;

if (numChange[8]>numCoin[8]) { numChange[8]=numCoin[8]};

numChange[7] = ((change - numChange[8]*10000)-(change%2000))/2000;

if (numChange[7]>numCoin[7]) { numChange[7]=numCoin[7]};

numChange[6] = ((change - numChange[8]*10000 - numChange[7]*2000)-(change%1000))/1000;

if (numChange[6]>numCoin[6]) { numChange[6]=numCoin[6]};

numChange[5] = ((change - numChange[8]*10000 - numChange[7]*2000-numChange[6]*1000)-(change%500))/500;

if (numChange[5]>numCoin[5]) { numChange[5]=numCoin[5]};

numChange[4] = ((change - numChange[8]*10000 - numChange[7]*2000-numChange[6]*1000-numChange[5]*500)-(change%100))/100;

if (numChange[4]>numCoin[4]) { numChange[4]=numCoin[4]};

numChange[3] = ((change - numChange[8]*10000 - numChange[7]*2000-numChange[6]*1000-numChange[5]*500-numChange[4]*100)-(change%25))/25;

if (numChange[3]>numCoin[3]) { numChange[3]=numCoin[3]};

numChange[2] = Math.round(((change - numChange[8]*10000 - numChange[7]*2000-numChange[6]*1000-numChange[5]*500-numChange[4]*100-numChange[3]*25)-(change%10))/10);

if (numChange[2]>numCoin[2]) { numChange[2]=numCoin[2]};

numChange[1] = ((change - numChange[8]*10000 - numChange[7]*2000-numChange[6]*1000-numChange[5]*500-numChange[4]*100-numChange[3]*25-numChange[2]*10)-(change%5))/5;

numChange[0] = ((change - numChange[8]*10000 - numChange[7]*2000-numChange[6]*1000-numChange[5]*500-numChange[4]*100-numChange[3]*25-numChange[2]*10-numChange[1]*5)-(change%1))/1;

return numChange;

}

}

numChange = sortCoin(change100, numCoin);

cashBackArr = [[“PENNY”, numChange[0]*0.01], [“NICKEL”, numChange[1]*0.05],[“DIME”, numChange[2]*0.10],[“QUARTER”, numChange[3]*0.25],[“ONE”, numChange[4]*1],[“FIVE”, numChange[5]*5],[“TEN”, numChange[6]*10],[“TWENTY”, numChange[7]*20],[“ONE HUNDRED”, numChange[8]*100]];

revCashBackArr = cashBackArr.slice();

revCashBackArr.reverse();

for (let k = 0; k < revCashBackArr.length;k++) {

if (revCashBackArr[k][1] !== 0) {

clearArr.push(revCashBackArr[k]);}

}

if(change100 > total100) {

return {status: “INSUFFICIENT_FUNDS”, change: };

}

if(change100 == total100) {

return {status: “CLOSED”, change: […cid]}

}

for (let i = 0; i < cashBackArr.length; i++) {

if (cashBackArr[i][1]>cid[i][1]) {

return ({status: “INSUFFICIENT_FUNDS”, change: });

}

}

if(change100 <= total100) {

return {status: “OPEN”, change: […clearArr]};

}

}

//heckCashRegister(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]]);

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

The most sticking out right now is the `sortCoin` function with all the `if`s. Intention to use loop there is a good one, but notice right now code in the loop would work without being inside of loop. As each `numChange` is calculated manually.

you are totally right.
I realized that I need a loop there, but couldn’t/didn’t need to use one for that function.
But I know it is just a manual calculation and I guess/feel I need a recursion there, which I couldn’t make up yet.

Now I will return back to the “Intermediate Algorithm Scripting” part to study more.
I hope it works and find a solution for it.

Thanks again.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.