Cash Register project challenge

Tell us what’s happening:

can someone please help me on this project challenge?

Your code so far


// Create an array of objects which hold the denominations and their values
var denom = [
  { 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 output = { status: null, change: [] };
  var change = cash - price;

  // Transform CID array into drawer object
  var register = cid.reduce(function(acc, curr) {
    acc.total += curr[1];
    acc[curr[0]] = curr[1];
    return acc;
  }, { total: 0 });

  // Handle exact change
  if (register.total === change) {
    output.status = 'CLOSED';
    output.change = cid;
    return output;
  }

  // Handle obvious insufficient funds
  if (register.total < change) {
    output.status = 'INSUFFICIENT_FUNDS';
    output.change = [];
    return output;
  }

  
  // Loop through the denomination array
  var change_arr = denom.reduce(function(acc, curr) {
    var value = 0;
    // While there is still money of this type in the drawer
    // And while the denomination is larger than the change remaining
    while (register[curr.name] > 0 && change >= curr.val) {
      change -= curr.val;
      register[curr.name] -= curr.val;
      value += curr.val;

     
    }
    // Add this denomination to the output only if any was used.
    if (value > 0) {
        acc.push([ curr.name, value ]);
    }
    return acc; // Return the current change_arr
  }, []); // Initial value of empty array for reduce

 
 
  // Here is your change, ma'am.
  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; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0.

Link to the challenge:

i am also stuck on this challenge im open to trying paired coding

I think you are close here. Look at what is failing I ran your code and got two failures (below)

The first error looks like it is not giving the correct change so I ran you code in JS Fiddle.net so I could isolate the test that is failing.

Try using

console.log(change);//debugging
console.log(value);//debugging

to debug your // Loop through the denomination array method I found it is not rounding the valuse so thats why it is failing you need some sort of rounding statement like:

// Round change to the nearest hundreth 'PENNY'.
      change = Math.round(change * 100) / 100;

The next error seems to show you are not passing insufficient funds error. After the loop, change_arr is a 2D array of the change due, sorted from highest to lowest denomination. If there are no elements in change_arr or you still owe change, return the output object with a status of INSUFFICIENT_FUNDS .

Hope this helps I had to use JS Fiddle to debug some of my code because I could run individual tests and put console.log statements where I could see what my code was doing.

I use Repl.it as well if you want to check out my solution I changed it a bit to include 1/2 dollar coins :slight_smile: FCC_Project: cashRegister.js - Replit

thanks mr. Johnny2136. it has worked out with your hint.

1 Like

Glad I could help. I hope you will find the online java script tools as valuable as I have.
https://codepen.io/
https://jsfiddle.net/
https://repl.it

and of course https://aws.amazon.com/cloud9/?origin=c9io which used to be alot better befor the AWS thing, but still nice.