JavaScript Algorithms and Data Structures Projects - Cash Register

Having a problem in solving the fifth part i.e. checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []} .
where should i change the code?
Your code so far

function checkCashRegister(price, cash, cid) {let stat = {status: "OPEN", change: []};
let tot = 0;
for (let coin of cid) {
  tot += coin[1];
}
let total = tot.toFixed(2);
let newarr = [];
let cd = JSON.parse(JSON.stringify(cid));

function changer(chan) {
  chan = Number(chan.toFixed(2));
  
  if (chan >= 100 && cd[8][1] >= 100) {
    cd[8][1] -= 100;
    changer(chan - 100);
  } else if (chan >= 20 && cd[7][1] >= 20) {
    cd[7][1] -= 20;
    changer(chan - 20);
  } else if (chan >= 10 && cd[6][1] >= 10) {
    cd[6][1] -= 10;
    changer(chan - 10);
  } else if (chan >= 5 && cd[5][1] >= 5) {
    cd[5][1] -= 5;
    changer(chan - 5);
  } else if (chan >= 1 && cd[4][1] >= 1) {
    cd[4][1] -= 1;
    changer(chan - 1);
  } else if (chan >= 0.25 && cd[3][1] >= 0.25) {
    cd[3][1] -= 0.25;
    changer(chan - 0.25);
  } else if (chan >= 0.1 && cd[2][1] >= 0.1) {
    cd[2][1] -= 0.1;
    changer(chan - 0.1);
  } else if (chan >= 0.05 && cd[1][1] >= 0.05) {
    cd[1][1] -= 0.05;
    changer(chan - 0.05);
  } else if (chan >= 0.01 && cd[0][1] >= 0.01) {
    cd[0][1] -= 0.01;
    changer(chan - 0.01);
  }
  
  newarr.push([cd[8][0],cid[8][1]-cd[8][1]],[cd[7][0],cid[7][1]-cd[7][1]],[cd[6][0],cid[6][1]-cd[6][1]],[cd[5][0],cid[5][1]-cd[5][1]],[cd[4][0],cid[4][1]-cd[4][1]],[cd[3][0],cid[3][1]-cd[3][1]],[cd[2][0],Number((cid[2][1]-cd[2][1]).toFixed(2))],[cd[1][0],cid[1][1]-cd[1][1]],[cd[0][0],Number((cid[0][1]-cd[0][1]).toFixed(2))]);
  return newarr;
}

let obj;
if (total < cash-price) {
  stat.status = "INSUFFICIENT_FUNDS";
  stat.change = [];
} else if (total == cash-price) {
  stat.status = "CLOSED";
  stat.change = cid;
} else {
  stat.status = "OPEN";
  obj = changer(cash-price);
  for (let i = 0; i<9;i++) {
    if (obj[i][1] != 0) {
    stat.change.push(obj[i]);
    }
  }
}
console.log(stat)
return stat;
}


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/114.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

Your code is pretty hard to read and reason about. For example, the push arguments.

Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return the exact change.

I didn’t look much at it but I would assume you are not handling the “or” part of the requirement.

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