Cash Register - Need Help

Cash Register - Need Help
0

#1

Tell us what’s happening:

I was able to pass the first 2 tests but cannot make logic for the other ones. Need help.

Your code so far


function checkCashRegister(price, cash, cid) {
  // Here is your change, ma'am.
  var sum = [];
  var temp = [];
  var real = [["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.1], ["QUARTER", 0.25], ["ONE", 1], ["FIVE", 5], ["TEN", 10], ["TWENTY", 20], ["ONE HUNDRED", 100]];

  var dollars = [];
  for (var x = 0; x < cid.length; x++) {
    dollars.push([cid[x][0], cid[x][1] * real[x][1]]);
  }

  console.log(dollars);
  
  var change = cash - price;
  console.log(change);

  for (var i = real.length-1; i >= 0; i--) {
    console.log(real[i][1]);
    while (real[i][1] < change) {
      real[i][1] += real[i][1];
      if (real[i][1] == change) {
        sum.push(real[i]);
      }
    }
  }
  return {"status" : "OPEN", "change" : sum};
}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]

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 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0.

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


#2

Ignore the dollars array for now.


#3

still need help. no reply yet.


#4

I see a few issues

-see comments in code

for (var i = real.length-1; i >= 0; i--) {
    console.log(real[i][1]);
    while (real[i][1] < change) {
      real[i][1] += real[i][1];  // this doubles real[i][1] every iteration
      console.log(real[i])
      if (real[i][1] == change) { // so this is unlikely to happen often
        sum.push(real[i]);
      }
    }
  }

It appears you are paying out of array real but aren’t checking if you actually have that much money in your drawer. For instance, you can’t give back four twenties in change if your cash drawer only has two twenties.

About if (real[i][1] == change) This is mathematically unlikely to happen. In the case where you owed back exactly $80 in change and you actually had $80 in twenties in cash drawer then it would work. But if you owed back $80.01 then real[i][1] could never equal change unless you have 8001 pennies.


#5

all I can think of is changing the while statement to:

while (real[i][1] < change && real[i][1] < cid[i][1])

but this doesn’t work either. I know it has something to do with my while loop which needs to be fixed. but cannot make the logic.


#6

I tried to change my loops like this:

for (var i = real2.length-1; i >= 0; i--) {
    while (real[i][1] < cid[i][1]) {
      real[i][1]++;
    }
    temp.push(real[i][1]);
  }

but still doesn’t do it for me.


#7

There are 3 possible return statements for 4 possible scenarios:

  • Your cid total is less than the change due
  • Your cid total is equal to the change due
  • Your cid total is large enough but can not make correct change
  • You are able to give correct change

You need to tackle each of these scenarios separately. I recommend starting with the easiest ones first:

let answer = {status: "", change: []};
  
  if (change > cidTotal) {
    answer.status = "INSUFFICIENT_FUNDS";
    return answer;
  }
  if (change== cidTotal) {
    answer.status = "CLOSED";
    answer.change = cid;
    return answer;

After these statements have run, you can think about tackling the 2 remaining scenarios.

You are on the right track when you get the quantities of each note as you will need them for your next loop.

I don’t want to give too many hints so see how you go from here, let me know if you have any follow up questions


#8

Ok. I think I’d be stuck at the same stage even if i start out with the easy possibilities but i’ll give it another try.


#9

It took me a few days of banging my head against the wall to pass this challenge too - you can do it!!


#10

That gives me hope. Thanks.


#12

I believe I am really close to the solution. Here’s my code:

function checkCashRegister(price, cash, cid) {
  var cidtotal = 0;
  var cid2 = [];
  var real2 = [];
  var sum = 0;
  var divider = [];

 /* var real = [["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.1], ["QUARTER", 0.25], ["ONE", 1], ["FIVE", 5], ["TEN", 10], ["TWENTY", 20], ["ONE HUNDRED", 100]];
 */

  var real = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];

  var change = cash - price;
  console.log(change);

  for (var x = 0; x < cid.length; x++) {
    cid2.push(cid[x][1]);
  }
  console.log(cid2);

  cidtotal = cid2.reduce(function(item, total) {
    return item + total;
  }, 0);

  console.log(cidtotal);

  var real2 = real.slice();
  console.log(real2);

  if (change > cidtotal) {
    return {"status" : "INSUFFICIENT_FUNDS", "change" : []};
  }

  else if (change == cidtotal) {
    return {"status" : "CLOSED", "change" : cid};
  }

  else if (change < cidtotal) {
    for (var y = 0; y < real.length; y++) {
        if (real[y] >= change) {
        real.splice(y);
        cid2.splice(y);
      }
    }
    console.log(real);
    console.log(cid2);
    for (var i = real.length-1; i >= 0; i--) {
      divider.unshift(Math.round(cid2[i] / real[i]));
      for (var j = 1; j < divider[i]; j++) {
        while (sum <= change) {
          sum = sum + (real[i] * j);
        }
      }
    }
    console.log(divider);
    console.log(sum);
  }

}

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

The problem is, the value of sum should be 96.74 but it’s giving me 96.75. I know I am missing something little. Can you help me point it out?