Cash register(Final JS Certification Project)

I have tried this code for two days it is not working. Can someone tell me where i have gone wrong?

My code so far


function checkCashRegister(price, cash, cid) {
var denom = [
{ name: "ONE HUNDRED", val: 100.0 },
{ name: "TWENTY", val: 20.0 },
{ name: "TEN", val: 10.0 },
{ name: "FIVE", val: 5.0 },
{ name: "ONE", val: 1.0 },
{ name: "QUARTER", val: 0.25 },
{ name: "DIME", val: 0.1 },
{ name: "NICKEL", val: 0.05 },
{ name: "PENNY", val: 0.01 }
];

function checkCashRegister(price, cash, cid) {
var output = { status: null, change: [] };
var change = cash - price;

var register = cid.reduce(
  function(acc, curr) {
    acc.total += curr[1];
    acc[curr[0]] = curr[1];
    return acc;
  },
  { total: 0 }
);


if (register.total === change) {
  output.status = "CLOSED";
  output.change = cid;
  return output;
}


if (register.total < change) {
  output.status = "INSUFFICIENT_FUNDS";
  return output;
}


var change_arr = denom.reduce(function(acc, curr) {
  var value = 0;
 
  while (register[curr.name] > 0 && change >= curr.val) {
    change -= curr.val;
    register[curr.name] -= curr.val;
    value += curr.val;
    change = Math.round(change * 100) / 100;
  }
  if (value > 0) {
    acc.push([curr.name, value]);
  }
  return acc; 
}, []); 
if (change_arr.length < 1 || change > 0) {
  output.status = "INSUFFICIENT_FUNDS";
  return output;
}
output.status = "OPEN";
output.change = change_arr;
return output;
}
}
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/83.0.4103.97 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

What errors are you seeing?

that last line in the console, undefined, is the output of your function: your function is not returning anything

@ieahleen can i get help with the code itself?

try debugging it yourself a bit, add console.log statements inside the function. Do your variables have the value you expect?

yes i do have the values

So you have two functions named checkCashRegister, one is defined inside the other but is never called. I believe we all do want to look into your code but it would also be nice if it had comments so that we can understand how much you also understand about what you are doing and as a beginner myself deciphering someone else’s code without comments feels like I’m drowning myself.

so do i remove the other checkCashRegister??
or do i call it?

It will not solve your issue if the logic of your solution is still not correct but I do sense the inner function is not meant to be there. So just remove it’s declaration (name and braces) but not its body.

how do you know this? have you checked?

yes i have: i think :thinking:

how have you checked, what method of debugging are you using?

for example,

so, if just below these lines you add console.log({output, change}), what does appear in the console?

yes i found the solution thank you @mphojele, @ilenia and @JeremyLT for responding to my query

3 Likes