JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:

Can someone explain me why my code does not pass this test?

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

Your code so far

function checkCashRegister(price, cash, cid) {
let change = {
  status: "OPEN",
  change: []
};
let changeMoney = 0;
let diff = cash - price;
cid.reverse()
console.log(diff);
let group=[];
cid.forEach(e => {
  let amount = getAmount(e[0]);
  let dziel = diff/amount;
    if(dziel >= 1){
      //group.push(amount);
      console.log(amount);
      console.log(group);
      console.log("---");
      let maxFromUnit = dziel*amount;
      changeMoney += maxFromUnit;
      change.change.push([e[0], maxFromUnit]);
      console.log("changeMoney");
      console.log(changeMoney);
      if(changeMoney == diff){
        console.log("noo");
        console.log(change);
        return;
        // return change;
      }
    }
})
  return change;
}

function getAmount(unit){
  switch(unit){
    case "PENNY":
    return 0.01;

    case "NICKEL":
    return 0.05;

    case "DIME":
    return 0.1;

    case "QUARTER":
    return 0.25;

    case "ONE":
    return 1;

    case "FIVE":
    return 5;

    case "TEN":
    return 10;

    case "TWENTY":
    return 20;

    case "ONE HUNDRED":
    return 100;
  }
}

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

My console log outputs:

{ status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }

I see there is spacing difference, but shouldn’t it pass anyway? Or how to transform current approach to be correct?

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

You are not returning { status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }, you are returning { status: 'OPEN', change: [ [ 'QUARTER', 0.5 ], [ 'DIME', 0.5 ], [ 'NICKEL', 0.5 ], [ 'PENNY', 0.5 ] ] }.

You cannot break out of a forEach loop.
You need to use another loop construct such as a for loop or a while loop.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.