[Cash Register] It's not working but everything is right

Tell us what’s happening:

Your code so far


function checkCashRegister(price, cash, cid) {
var denominations = [
  { name: 'ONE HUNDRED', val: 100.00},
  { name: 'TWENTY', val: 20.00},
  { name: 'TEN', val: 10.00},
  { name: 'FIVE', val: 5.00},
  { name: 'ONE', val: 1.00},
  { name: 'QUARTER', val: 0.25},
  { name: 'DIME', val: 0.10},
  { name: 'NICKEL', val: 0.05},
  { name: 'PENNY', val: 0.01}
];

function checkCashRegister(price, cash, cid) {
 
  var change = cash - price;

  var totalCid = cid.reduce(function(acc,next){
    return acc + next[1];
  },0.0);

  if (totalCid < change){
    return 'Insufficient Funds';
  } else if (totalCid === change){
    return 'Closed';
  }

  cid = cid.reverse();

  var result = denominations.reduce(function(acc,next,index){
    if(change >= next.value){
      var currentValue = 0.0;
      while(change >= next.value && cid[index][1] >= next.value){
        currentValue += next.value;
        change -= next.value;
        change = Math.round(change * 100) / 100;
        cid[index][1] -= next.value;
      }
      acc.push([next.name,currentValue]);
      return acc;
      } else {
        return acc;
      }
  },[]);

return result.length > 0 && change === 0 ? result : 'Insufficient Funds';
}
}






checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0.

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

What is going on? What are you trying to do? What part do you need help with? What errors are you experiencing? What else have you tried while attempting to solve the problem?

1 Like

When I click on "Run the Tests " it is not showing anything. It is showing a problem with the semicolon of ```
checkCashRegister(19.50, 20.00, [[“PENNY”, 1.01], [“NICKEL”, 2.05], [“DIME”, 3.10], [“QUARTER”, 4.25], [“ONE”, 90.00], [“FIVE”, 55.00], [“TEN”, 20.00], [“TWENTY”, 60.00], [“ONE HUNDRED”, 100.00]]);

Wait a minute… you’re declaring a function checkCashRegister inside the function checkCashRegister, which takes in the same arguments. Kinda unconventional, but okay…

the second checkCashRegister function is declared but I don’t think it’s doing anything. You close it off with " },[]);" I’m new to Javascript, so maybe this is a legit syntax, but if you’re trying to do an immediately invoked function, I believe it takes this form:

(function(){
//body
} )();

Also, it helps during the debugging process to console.log() all your variables, arrays etc at every step of the code, to make sure every value is as it should be. When you reach the point where console.log() shows a weird result, that’s where you know you’ve got a problem. For example, if you console.log() anything within the second checkCashRegister function, nothing prints. That’s because nothing inside that function is being executed.

Thank’s a lot.
I really didn’t check for that.