Cash Register Help Open Checks not completing

Tell us what’s happening:
Hi Guys,

Currently trying to tackle this last problem. All the checks are complete bar the OPEN ones.
Check 2 is coming out as two quarters which is fine but i dont know how to feed this into a final array.

Check 3 however is coming up totally wrong. Could anyone help me troubleshoot my code?

Your code so far


function checkCashRegister(price, cash, cid) {
 var cashAvailable = cid;
 var units = [{name:"PENNY", value:0.01}, {name:"NICKEL", value:0.05}, {name:"DIME", value: 0.1}, {name:"QUARTER",value: 0.25}, {name:"ONE", value:1}, {name:"FIVE", value:5}, {name:"TEN", value:10}, {name:"TWENTY",value: 20}, {name:"ONE HUNDRED", value: 100}].sort((a,b)=>{return b.value - a.value})

var register = {status:'', change:[]};
var changeRequired = cash - price;
var total = cashAvailable.flat().filter(x=>{return isNaN(x) == false}).reduce((a,b)=>{return a+b}).toFixed(2);

if(total < changeRequired){
  register.status = 'INSUFFICIENT_FUNDS'
  return register
} else if(total == changeRequired){
  register.status = 'CLOSED'
  register.change = cashAvailable
  return register
}

if(total > changeRequired){
  for(var i=0; i<units.length; i++){
    while(changeRequired >= units[i].value && cashAvailable[i][1] > 0){
      console.log(Math.round(changeRequired -= units[i].value))
      cashAvailable[i][1] -= units[i].value
    }
  }
  (changeRequired == (cash-price))? register.status = 'INSUFFICIENT_FUNDS' : (register.status = 'OPEN', register.change )

  console.log(changeRequired)
}

return register

}

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]])

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

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

Couple things to note - you have three possible exclusive logic branches operating here. Either the register doesn’t have enough money, the register has exactly enough money, or the register has enough money. If one of those three are true, the other two can’t be. So given that, I would remove the early return statements in each branch, as that makes debugging a little more difficult.

That, however, is unrelated to your problem. Have you actually used console.log() to view what the register looks like before it’s returning? In every case of the third branch (enough money, but maybe or maybe not in the right configuration), the logic is failing and it is returning the ‘INSUFFICIENT FUNDS’ case. Every time.

Another issue you seem to be having is, you’ve sorted your units array in descending order, but you’re still accessing the cash register in ascending order.

Okay so I’ve made the changes you’ve stated. I did use the console log to check, I think I made some changes which causes it to keep outputting insufficient funds.

Now I get approximately the correct output except for the second check it won’t take away the final penny.

Also, I give the array broken down into single change required and not the total amount of each. How can i give the final array for open as a total of each unit?

Your code so far

function checkCashRegister(price, cash, cid) {

  var cashAvailable = cid;

  var units = [{name:"PENNY", value: 0.01}, {name:"NICKEL", value:0.05}, {name:"DIME",value: 0.1}, {name:"QUARTER",  value: 0.25}, {name:"ONE",value: 1}, {name:"FIVE", value:5}, {name:"TEN", value:10}, {name:"TWENTY",value: 20},    {name:"ONE HUNDRED", value:100}].reverse()

  var cashAvailable = cid.slice().reverse().map(function(x){return {name:x[0], value:x[1]}})
  var stat = {status:'', change:[]};
  var changeRequired = cash - price;
  var totalCash = cashAvailable.map(x=>{return x.value}).reduce((a,b)=>{return a+b})
  
  if(totalCash == changeRequired){
    stat.status = 'CLOSED'
    stat.change = cid
    return stat
  }

  if (totalCash < changeRequired){
    stat.status = 'INSUFFICIENT_FUNDS'
    stat.change = []
    return stat
  } 

  var finalArr = []
  for(var i = 0; i<units.length; i++){
    while(changeRequired >= units[i].value && cashAvailable[i].value > 0){

      finalArr.push(units[i].name)
      cashAvailable[i].value = Math.round((cashAvailable[i].value*100)/100) - units[i].value
      console.log(changeRequired -= units[i].value)

    }
  }

console.log(changeRequired)
console.log(finalArr)

return stat

}

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]])

Word to the wise? Don’t post solutions to challenges here. Don’t make it easier for the next person to simply take your code and use it as your own.

For the lessons leading up to the challenge, if you post them, blur them out (click on the gear just above, and select ‘Blur Spoiler’).

My apologies, I’m quite new to the code camp platform :sweat_smile:

And you’re doing incredible things with it. No worries, simply a suggestion that will let others have the same joy of discovery you had.

And and AND!!! Now that you’ve beaten your head on that particular wall, and found how to make it work, when the NEXT person posts a "Hey, help - my cash register challenge just ***insert fubar here***", you’re in a GREAT place to help them now! Yaaaaay!!