Cash Register -It does not display what is needed

I have a mistake that I don't realize. When I call checkCashRegister it should appear in my case: {status: "INSUFFICIENT_FUNDS", change: []}, but this appears to me: {status: 'OPEN', change: [['PENNY', 0.01]]}

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”, 1], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]])

Hello there.

Do you have a question?

If so, please edit your post to include it.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

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