What is Wrong With My Code? Cash Register Project

Tell us what’s happening:

Your code so far


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;
 }
 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;
}

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; rv:76.0) Gecko/20100101 Firefox/76.0.

Challenge: Cash Register

Link to the challenge:

I can not run the test on my code because there are some problems with my code. Could you please help?

Hello~!

Couple of things I noticed.

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

You have incorrect syntax here - you’ve written .reduce{stuff} when it should be .reduce(stuff).

When I fix that, I get this error:
image

1 Like

Doesn’t denom stand for “denominator”?

In a mathematical lexicon, sure.
In JavaScript, no.

1 Like

ohhh, okay. Thank you.

I just took a peak at hint and “denom” is supposed to be in my code. So what else is wrong?

The hint likely declares denom earlier in the code. You haven’t declared denom.

Remember that variables are declared with var, let, or const, though var is falling into disuse.

1 Like

Your right! I might have it now. Just a minute!

@ieahleen
It won’t let me copy and paste it!

Tell us what’s happening:

Your code so far


var denom = [
{name: "ONE HUNDRED", val: 100.0},
{name: "TWENTY", val: 60.0},
{name: "TEN", val: 20.0},
{name: "FIVE", val: 55.0},
{name: "ONE", val: 90.0},
{name: "QUARTER", val: 4.25},
{name: "DIME", val: 3.1},
{name: "NICKEL", val: 2.05},
{name: "PENNY", val: 1.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;
 }
 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;
}

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; rv:76.0) Gecko/20100101 Firefox/76.0.

Challenge: Cash Register

Link to the challenge:

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]]) should return {status: "OPEN", change: [["QUARTER", 0.5]]} .

and

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]]) should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}

are coming in as wrong. But everything else is right. Please help me!

I believe i have spelled everything correctly…What is wrong?

Those two tests are returning this:
image
A little bit of digging shows that:


You’re returning insufficient if the change value is greater than 0.
The code isn’t pushing to the change_array correctly.

Tell us what’s happening:

Your code so far


var denom = [
{name: "ONE HUNDRED", val: 100.0},
{name: "TWENTY", val: 60.0},
{name: "TEN", val: 20.0},
{name: "FIVE", val: 55.0},
{name: "ONE", val: 90.0},
{name: "QUARTER", val: 4.25},
{name: "DIME", val: 3.1},
{name: "NICKEL", val: 2.05},
{name: "PENNY", val: 1.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;
}

checkCashRegister(19.5, 20.0, [
["PENNY", 1.01],
["NICKEL", 2.05],
["DIME", 3.1],
["QUARTER", 4.25],
["ONE", 90.0],
["FIVE", 55.0], 
["TEN", 20.0],
["TWENTY", 60.0],
["ONE HUNDRED", 100.0]
]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:76.0) Gecko/20100101 Firefox/76.0.

Challenge: Cash Register

Link to the challenge:

Because i could not understand what is wrong, I went to hint and studied the answer supplied there. I made some small changes to my code and now my code and the solution is identical. Unfortunately, it is still coming in wrong. Is something wrong with my computer?

image

Looks like your issue is with these values. Why did you choose these values?

1 Like

Awww, crap! That’s where I messed up. I am being so stupid today! maybe sleeping in isn’t always the best plan. :slightly_smiling_face:

All fixed now and everything is right. Thank you for being patient with me!

Congratulations! You did it! Was that your last JS project?

1 Like

I think so. Am i supposed to get a certificate? Because it is just moving me on to bootstrap.

Nevermind! I just claimed my certificate! yay!

1 Like