JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Describe your issue in detail here.
My output is the same as required. I can’t understand what is the problem

  **Your code so far**
let sub = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
let result = {};

function checkCashRegister(price, cash, cid) {
let hundredPrice = price * 100;
let hundredCash = cash * 100;
let hundredCid = cid.map(item =>{
 return [item[0], item[1] * 100];
})
let hundredSdacha = hundredCash - hundredPrice;
recursion(hundredSdacha, hundredCid);
if(!(result.status == "INSUFFICIENT_FUNDS")) {
if(!hundredCid.map(item => item[1] == 0 ? undefined : item).filter(e => e).length == 0) {
  result.status = "OPEN";
result.change = sub.map(item => item[1] == 0 ? undefined : [item[0],item[1]/100]).filter(e => e).reverse();
return result;
} else {
  result.status = "CLOSED";
  result.change = sub.map(item => [item[0],item[1]/100]).filter(e => e);
  return result;
    }
} else {
  result.change = [];
}
return result;
}
function recursion(sdacha, cid) {
if(sdacha >= 10000 && cid[8][1] >=10000 ) {
sdacha -= 10000;
cid[8][1] -= 10000;
sub[8][1] += 10000;
recursion(sdacha, cid);
} else if(sdacha < 10000 && sdacha >= 2000 && cid[7][1] >=2000 || sdacha >= 10000 && cid[8][1] < 10000 && cid[7][1] >=2000) {
sdacha -= 2000;
cid[7][1] -= 2000;
sub[7][1] += 2000;
recursion(sdacha, cid);
} else if(sdacha < 2000 && sdacha >= 1000 && cid[6][1] >=1000 || sdacha >= 2000 && cid[7][1] < 2000 && cid[6][1] >=1000 ) {
sdacha -= 1000;
cid[6][1] -= 1000;
sub[6][1] += 1000;
recursion(sdacha, cid);
} else if(sdacha < 1000 && sdacha >= 500 && cid[5][1] >=500 || sdacha >= 1000 && cid[6][1] < 1000 && cid[5][1] >=500) {
sdacha -= 500;
cid[5][1] -= 500;
sub[5][1] += 500;
recursion(sdacha, cid);
} else if(sdacha < 500 && sdacha >= 100 && cid[4][1] >=100 || sdacha >= 500 && cid[5][1] < 500 && cid[4][1] >=100) {
sdacha -= 100;
cid[4][1] -= 100;
sub[4][1] += 100;
recursion(sdacha, cid);
} else if(sdacha < 100 && sdacha >= 25 && cid[3][1] >=25 || sdacha >= 100 && cid[4][1] < 100 && cid[3][1] >=25) {
sdacha -= 25;
cid[3][1] -= 25;
sub[3][1] += 25;
recursion(sdacha, cid);
} else if(sdacha < 25 && sdacha >= 10 && cid[2][1] >=10 || sdacha >= 25 && cid[3][1] < 25 && cid[2][1] >=10) {
sdacha -= 10;
cid[2][1] -= 10;
sub[2][1] += 10;
recursion(sdacha, cid);
} else if(sdacha < 10 && sdacha >= 5 && cid[1][1] >=5 || sdacha >= 10 && cid[2][1] < 10 && cid[1][1] >=5) {
sdacha -= 5;
cid[1][1] -= 5;
sub[1][1] += 5;
recursion(sdacha, cid);
} else if(sdacha < 5 && sdacha >= 1 && cid[0][1] >=1 || sdacha >= 5 && cid[1][1] < 5 && cid[0][1] >=1) {
sdacha -= 1;
cid[0][1] -= 1;
sub[0][1] += 1;
recursion(sdacha, cid);
 } else if(sdacha < 1) {
  result.status = "KAIF";
}
else {
  result.status = "INSUFFICIENT_FUNDS";

}
} 
console.log(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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

Your code contains global variables that are changed each time the function is run. This means that after each function call completes, subsequent function calls start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

Note that this problem isn’t really a good match for recursion.

You can

  1. fix you recursion to use the return value

  2. change your recursion to hide the return value in some default parameter instead of a global variable

  3. reward the logic to avoid recursion

I recommend 3

1 Like

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