Cash Register wont accept result

Hello, I finished my cash register project and its returning the expected results but FCC says its not. All my outputs are identical to the expected outputs but I am still unable to pass. What should I do, is there something I am not seeing? Here is my code sorry its messy.

function checkCashRegister(price, cash, cid) {
  let arr = [...cid];
  let money = cash - price;

  let index;
  let value;
  let divide;
  let changeValue;
  let change = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
  let amount = [["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.10], ["QUARTER", 0.25], ["ONE", 1.00], ["FIVE", 5.00], ["TEN", 10.00], ["TWENTY", 20.00], ["ONE HUNDRED", 100.00]];
  for(let i=0; i<arr.length; i++){
    if (arr[i][1]<amount[i][1]){
      arr.splice(i,1);
      amount.splice(i,1);
      i-=1
    }
  }
  //calculate

  //starting point for for loop (from the end)
  let start = amount.length - 1;

  for (let i=start; i>=0; i--){
    value = arr[i][1];
    changeValue = 0;
    if (amount[i][1]<=money || value > 0){
      divide = money/amount[i][1];
      divide = Math.trunc(divide);
      for (let x=1; x<=divide; x++){
          changeValue+=amount[i][1];
          change[i][1] = changeValue;
          value = parseFloat((value - amount[i][1]).toFixed(2));
          arr[i][1] = value;
          money = parseFloat((money - amount[i][1]).toFixed(2));
          if (value <= 0){
            break
          }
        }
      }
    }
  

  //fix decimals 
  let numDec;
  for (let i=0; i<change.length; i++){
    if (change[i][1]>0.00){
      numDec=change[i][1];
      numDec = numDec.toFixed(2).replace(/\.?0+$/, "");
      change[i][1] = numDec;
    }
  }

  //result log
  let counter=0;
  let empty=false;
  let changeBack = [];
  for (let i=start; i>=0; i--){
    if(change[i][1]!=0){
      changeBack.push(change[i]);
    }
  }

  for (let i=0; i<arr.length; i++){
    if (arr[i][1] == 0){
      counter++;
    }
  }
  if (counter==arr.length){
    empty=true;
  }

  let open = {status: "OPEN", change: changeBack};
  let closed = {status: "CLOSED", change: change};
  let insufficient = {status: "INSUFFICIENT_FUNDS", change: []}

  if (money == 0 && empty == false){
    return open;
  }


  if (money == 0 && empty == true){
   return closed;
  }
  
  if (money > 0){
    return insufficient;
  }

}

console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));


User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

Which tests are failing?

I can see where it’s going wrong. The expected output for the 2nd index in the array is a number, and yours is outputting a string.

This is a super easy fix, and it’s on this line:

change[i][1] = numDec;

When you call toFixed, the value is converted to a string. All you have to do at this point is parse a floating number from numDec and it passes all tests.

change[i][1] = parseFloat(numDec, 10);
2 Likes

That was the solution. Thank you so much!

My pleasure! Happy Coding! =]

1 Like

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