Cash Register not passing one test

Tell us what’s happening:
I cannot seem to pass the second to last test for the cash register. My output appears to be as it should.

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", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

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: []} .

its where it goes wrong as i run it

did u try and use the

1 Like

I know where it goes wrong. I just do not know why the test is not accepting it, because, as far as I can tell, the output is the same as what the check requires.

I have looked at the lesson help, but I do not want to copy paste a completely different solution.

Instead of returning {status: "INSUFFICIENT_FUNDS", change: []} you are returning { status: 'OPEN', change: [ [ 'PENNY', 0.01 ] ] }.

1 Like

Hi @ArielLeslie how do u modify the code so that it returns insufficient funds instead

Hi @ArielLeslie… How do you modify the code so that it returns insufficient funds

1 Like

Hello Hlulani,

In the future, please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.

The easiest way to create a topic for help with your own solution is to click the Ask for Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge url while still allowing you to ask any question about the challenge or your code.

Thank you.