My Cash Register isn't registering correctly

Tell us what’s happening:
Something is happening in part 3 of my code, where I am attempting to use a switch statement to calculate the drawer. I tried it yesterday and didn’t have this problem but as I was working my internet crashed, the page reloaded itself, and I lost everything I did. I can’t entirely remember what I wrote inside my switch statement, but it was passing tests #2 and #3.

Your code so far


function checkCashRegister(price, cash, cid) {

  /*>>> */ // 1. FUNCTION SET UP /* <<<*/
  var change = [];
  var total = 0;
  var curr = 0;
  var changeDue = cash - price; 
  var obj = { status: status, change: change};
    
  /*>>> */ // 2. LOOP TO CALCULATE TOTAL CASH ON HAND /* <<<*/
  for (let i=0; i < cid.length; i++) {
    total += cid[i][1];      
  }  
  total =  Number.parseFloat(total).toFixed(2);
  
/*>>> */ // 3. LOOP TO CALCULATE CHANGE BREAKDOWN /* <<<*/
    for (let r=cid.length-1; r > 0; r--) {
     curr = cid[r][1];     
     var num = (curr % changeDue); 
     num = Number.parseFloat(num).toFixed(2);
     
     switch (num) {
       case 0: r--; break;
       case .01: curr -= changeDue; change.push(cid[r]); break;
       case .05: curr -= changeDue; change.push(cid[r]); break;
       case .10: curr -= changeDue; change.push(cid[r]); break;
       case .25: curr -= changeDue; change.push(cid[r]);  break;
       case 100: curr -= changeDue; change.push(cid[r]); break;
       case 20: curr -= changeDue; change.push(cid[r]);  break;
       case 10: curr -= changeDue; change.push(cid[r]); break;
       case 5: curr -= changeDue; change.push(cid[r]);break;
       case 1: curr -= changeDue; change.push(cid[r]); break;      
     }
    }
    
  
  /*>>> */ // 4. CONDITION STATEMENT TO UPDATE STATUS /* <<<*/
      if (num > 0.01) { 
        obj.status = "OPEN"; obj.change = change;
      } else if (total == changeDue) {
        obj.status = "CLOSED"; obj.change = cid;
      } else {obj.status = "INSUFFICIENT_FUNDS"; obj.change = [];  }

  return obj; 
}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]
console.log(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/70.0.3538.77 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/

Am I missing something, or do all of your switch statements execute the same actions?

No, just 9 out of 10 of them. :smile:

Thank you for replying. The logic I am attempting to run is that if variable num is equal to one of the case numbers run to let the value of cid[r][1] to equal less the amount of change (updating the register). Then, to push the value of cid[r] to the change array.

Ouch! Maybe suggesting a solution is more useful than laughing at someone asking for help.

If you want to apply the same logic for multiple cases, I suggest reviewing this previous challenge.

I was not laughing at you. I simply smiled at @ArielLeslie about her statement.

Hi FCC, If anyone can answer the following, it would be much appreciated:

  1. What’s wrong with/missing from my switch statement? (somehow I passed challenges 2 & 3 yesterday, but I can’t remember what I wrote and I didn’t save before my pc crashed)
  2. When num === 0.25, why does it not output the value of the current index (cid[r]), which is [“QUARTERS”, 4.25]?
  3. Why doesn’t every type of currency log into the console when it’s run? (On multiple attempts, it runs ONE HUNDRED to QUARTER, but is often missing the lower currencies).

Thanks :slight_smile:

Before your switch, put in a console.log(typeof num). See what information that gives you.