JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Describe your issue in detail here.
Hello Campers,
I’m kind of stuck at test 3 of this challenge, I’m unable to find the error because:

  1. The code executes perfectly for test 3 if the “cash” value in the function is kept below 68. When it is increased from 68 this code executes the “INSUFFICIENT_FUND” condition. Any insights would be of great help.

Thanks
nitin

Your code so far

function checkCashRegister(price, cash, cid) {
  let x1 = parseFloat((price*100)/100);
  let x2 = parseFloat((cash*100)/100);
  const a = JSON.parse(JSON.stringify(cid)), newarr =[];
  let sum = 0;
  for (let i=0; i < 9 ; i++){
    sum += a[i][1]; 
  }
  //sum = Math.round(sum*100)/100;
  console.log("Total fund that I have with me (sum)= "+ sum);
  let roundChange = (cash - price);
  console.log("Funds that i need to give back (roundChange)= "+ roundChange);
  console.log(typeof(sum));console.log(typeof(roundChange));
  let stat = {
     status: "",
     change: []
     };

  if (roundChange < sum){
    function changer(chan){
        if (chan >= 100 && a[8][1] >= 100) {
          a[8][1] -= 100;
          changer(chan - 100);
        } else if (chan >= 20 && a[7][1] >= 20) {
           a[7][1] -= 20;
          changer(chan - 20);
        } else if (chan >= 10 && a[6][1] >= 10) {
           a[6][1] -= 10;
          changer(chan - 10);
        } else if (chan >= 5 && a[5][1] >= 5) {
           a[5][1] -= 5;
          changer(chan - 5);
        } else if (chan >= 1 && a[4][1] >= 1) {
           a[4][1] -= 1;
          changer(chan - 1);
        } else if (chan >= 0.25 && a[3][1] >= 0.25) {
           a[3][1] -= 0.25;
          changer(chan - 0.25);
        } else if (chan >= 0.1 && a[2][1] >= 0.1) {
           a[2][1] -= 0.1;
          changer(chan - 0.1);
        } else if (chan >= 0.05 && a[1][1] >= 0.05) {
           a[1][1] -= 0.05;
          changer(chan - 0.05);
        } else if (chan >= 0.01 && a[0][1] >= 0.01) {
           a[0][1] -= 0.01;
          changer(chan - 0.01);
        }
        newarr.push([a[8][0],cid[8][1]-a[8][1]],
        [a[7][0],cid[7][1]-a[7][1]],
        [a[6][0],cid[6][1]-a[6][1]],
        [a[5][0],cid[5][1]-a[5][1]],
        [a[4][0],cid[4][1]-a[4][1]],
        [a[3][0],cid[3][1]-a[3][1]],
        [a[2][0],Math.round((cid[2][1]-a[2][1])*100)/100],
        [a[1][0],cid[1][1]-a[1][1]],
        [a[0][0],Math.round((cid[0][1]-a[0][1])*100)/100]
        );
  return newarr;
      
      }

     let obj = changer(roundChange);
     //console.log(obj);
     for (let i = 0; i<9;i++) {
        if (obj[i][1] != 0) {
            stat.change.push(obj[i]);
          }
       }
       let newChange = 0;
       console.log(stat.change)
       for (let j=0; j<stat.change.length; j++){
          newChange += stat.change[j][1];
       }
       
       console.log(newChange);

       if(newChange == Math.round(roundChange*100) / 100){
          stat.status = 'OPEN';
          }
       else if(newChange < Math.round(roundChange*100) / 100){
         stat.status = 'INSUFFICIENT_FUNDS';
         stat.change=[];
         }
}
     else if(roundChange > sum){
      stat.status = "INSUFFICIENT_FUNDS";
      stat.change = [];
  }
    else if (roundChange ===sum){
      stat.status = "CLOSED";
      stat.change = a;
  }

return stat;
  
}

console.log(checkCashRegister(3.26, 68, [["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/113.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

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