Tell us what’s happening:
Hi everyone, I’ve been stuck on this test for three days now. This is the code I wrote according to my idea, but it doesn’t run successfully. It only returns: Array [ “ONE HUNDRED”, 0 ] each time
Thank you all.

``````   **Your code so far**
``````
``````
function checkCashRegister(price, cash, cid) {
var change=cash-price; //Change should be given
var sumCid=0;   //How much money is in the cash register in total
var realChange=[];  //The final returned two-dimensional array of change
var unitNum=[0.01,0.05,0.1,0.25,1,5,10,20,100] //Denomination
var n=0;  //Maximum number of change for the same denomination
for (let i=0;i<cid.length;i++) {
sumCid+=cid[i]   //figure out sumcid
}
if (cash>sumCid) {  //If you can't change it at the beginning, return to
return {status: "INSUFFICIENT_FUNDS", change: []};
}else if (cash===sumCid) {  //If you can find the change directly, return
return {status: "CLOSED", change: [cid]};
}else if (cash<sumCid) {  //If the total amount in the cash register can cover change
for (var i=unitNum.length - 1;i>=0;i--) {  //Reverse traversal of par value arrays
if (change>=unitNum[i]&&change<=cid[i]) {  //Find the denomination of change just greater than or equal to and the current denomination has that much change
for (let n=0;n<=(cid[i]/unitNum[i]);n++) { //Find the exact size of the n
//Change minus the most money you can find for the current denomination
return change=change-unitNum[i]*n;
}
}
return realChange.concat([cid[i],unitNum[i]*n])//Concatenate the denominations and values of the found change into a two-dimensional array.
}
}
else if (change===0) {
return {status: "OPEN", change: realChange}
}

}
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]]);
``````
``````   **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0`

Challenge: Cash Register

Link to the challenge:

You have a `return` in the middle of your code. and a return will return whatever value you put in it and stop the function right there.

``````else if (cash<sumCid) {  //If the total amount in the cash register can cover change
for (var i=unitNum.length - 1;i>=0;i--) {  //Reverse traversal of par value arrays
if (change>=unitNum[i]&&change<=cid[i]) {  //Find the denomination of change just greater than or equal to and the current denomination has that much change
for (let n=0;n<=(cid[i]/unitNum[i]);n++) { //Find the exact size of the n
//Change minus the most money you can find for the current denomination
//
return change=change-unitNum[i]*n;
//This ^^^ will probably be a problem later on
}
}

//
return realChange.concat([cid[i],unitNum[i]*n])//Concatenate the denominations and values of the found change into a two-dimensional array.
//This return here is our problem ^^^ for now
``````

So its getting here and sending that value, then stopping the function.

You also have some `var`'s in there. You are better off using `let` and `const` at this point. For example `var unitNum` is your denomination amounts, which should not be changed, and is better off being set with a `const`.

1 Like

thank you.i will try to change my code.

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