Cash Register.I am returning that right answer for challenge #2 but

Cash Register.I am returning that right answer for challenge #2 but
0.0 0

#1

Tell us what’s happening:

i have returned the answer in the console and it shows the right answer. However if i try it on freecodecamp, it does not pass.

Warning-- for the third test. this code will go into an infinite loop. I don’t know why just yet.

the main problem-- Why is it not passing the second test on the cash register challenge

Your code so far


    function checkCashRegister(price, cash, cid) {
var cashAvailable = 0;
var changeOwed = cash-price;
var change = {
    'ONE HUNDRED':100,
    'TWENTY':20,
    'TEN':10,
     'FIVE':5,
     'DOLLAR':1,
     'QUARTER':0.25,
     'DIME':0.1,
     'NICKEL':0.05,
     'PENNY':0.01
}

var yourChange = {
    status:"OPEN",
    change: []
}

var noFunds = {
    status:"INSUFFICIENT_FUNDS",
    change: []
    };


var closed = {
    status:"CLOSED",
    change:cid
    };


var flatt = cid.reduce(
    function(acc, curr){
    return acc.concat(curr);
    })
    
for(var i = 0; i<flatt.length; i++){
    if(typeof flatt[i] === "number"){
        cashAvailable += flatt[i];
    }
}
cashAvailable = Math.round(100*cashAvailable)/100

    if(cashAvailable < changeOwed){
        return noFunds;
    }else if(cashAvailable === changeOwed){
        return closed;
    }
    //the below code is for coins only and changeOwed is less than one dollar. 
var coins = 0;
    for(var i = 3; i>=0;i--){
    
        if(cid[i][1] > 0.00){
            coins += cid[i][1];
        }     
    }
    if(changeOwed > coins && changeOwed < 1.00){
        return noFunds;
    };
    //the below code if for the actual change returned
        var ans = [];
    var compiler = 0;
        for(var prop in change){
        var check = changeOwed - change[prop];
        if( check < changeOwed && check > 0){
           for(var i = cid.length -1; i > -1;i--){
             if(cid[i][0]=== prop){
                 var changeAvailable = cid[i][1] 
                  while(changeAvailable !== 0 && changeOwed !== 0){
                      
                      changeAvailable = changeOwed - change[prop];
                      changeOwed = changeOwed - change[prop];
                      compiler +=change[prop];
                      if (changeAvailable === 0 || changeOwed <= 0 ){
                        ans.push([prop,compiler]);
                      
                      }
                  }
                 yourChange.change.push(ans); 
             }
            }             
        }
     }
     return yourChange;    
}
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 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

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


#2

I don’t know, I can’t get the test console to run at all, it just locks up. I don’t see the results from any test.

I would take care of the infinite loop first. What is the point of bandaiding a finger if you may have to cut off the arm?


#3

For test three what is happening is you subtract a 20 from 16 and some change. You need to check and make sure that you’re not going past zero. You’re only checking to see if it is not equal to zero. In this case, it never is equal to zero, which is causing the infinite loop. That might also help you fix your other issues.


#4

The way I approach these challenges though is by passing one test at a time. I figure if i can pass one test out of the three than I must be on the right track. My problem was the while loop. Thank’s to the both of you for taking a look at my code. my final solution is below. Let me know how you would have done it. I know it a monster.

function checkCashRegister(price, cash, cid) {
  var cashAvailable = 0;
  var changeOwed = cash - price;
  var change = {
    "ONE HUNDRED": 100,
    TWENTY: 20,
    TEN: 10,
    FIVE: 5,
    ONE: 1,
    QUARTER: 0.25,
    DIME: 0.1,
    NICKEL: 0.05,
    PENNY: 0.01
  };

  var yourChange = {
    status: "OPEN"
  };

  var yourChange = {
    status: "OPEN",
    change: []
  };

  var noFunds = {
    status: "INSUFFICIENT_FUNDS",
    change: []
  };

  var closed = {
    status: "CLOSED",
    change: cid
  };

  var flatt = cid.reduce(function(acc, curr) {
    return acc.concat(curr);
  });

  for (var i = 0; i < flatt.length; i++) {
    if (typeof flatt[i] === "number") {
      cashAvailable += flatt[i];
    }
  }
  cashAvailable = Math.round(100 * cashAvailable) / 100;

  if (cashAvailable < changeOwed) {
    return noFunds;
  } else if (cashAvailable === changeOwed) {
    return closed;
  }
  //the below code is for coins only and changeOwed is less than one dollar.
  var coins = 0;
  for (var i = 3; i >= 0; i--) {
    if (cid[i][1] > 0.0) {
      coins += cid[i][1];
    }
  }
  if (changeOwed > coins && changeOwed < 1.0) {
    return noFunds;
  }
  //the below code if fo the actual change returned

  var ans = [];
  var compiler = 0;
  for (var prop in change) {
    var check = changeOwed - change[prop];
    if (check < changeOwed && check > 0) {
      for (var i = cid.length - 1; i > -1; i--) {
        if (cid[i][0] === prop) {
          var changeAvailable = cid[i][1];
          console.log(changeAvailable);
          while (
            changeAvailable > 0 &&
            changeOwed > 0 &&
            changeOwed >= change[prop]
          ) {
            changeAvailable = changeAvailable - change[prop];
            changeOwed = changeOwed - change[prop];
            compiler += change[prop];
            changeOwed = Math.round(100 * changeOwed) / 100;
          }
          ans.push([prop, compiler]);
          compiler = 0;
        }
      }
    }
  }
  yourChange.change = ans;

  return yourChange;
}
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]
]);


#5

I figured it out. Thank you for looking at my code though. You were right that was apart of the problem.


#6

I’ll send you a private message with the solution I would have used and some comments in your code so that you can see where you can make changes to make it more efficient and easier to work with.