Help with Meeting Test Cases for Cash Register Exercise in JS Certification

Tell us what’s happening:
Hi reader! Hope all is well with you. I am writing to ask for help about the test two test cases in the Cash Register exercise where you return the change to the customer.

When I log the ‘result’ variable with the output it matches the expected output from the exercise, however my output is marked as wrong for some reason.

Can anyone help me?

Test Case 1
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]])

// Expected result: {status: “OPEN”, change: [[“QUARTER”, 0.5]]}

Test Case 2
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]])

// Expected result: {status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}

Your code so far


const denom = [
{ name: "ONE HUNDRED", value: 100.0 },
{ name: "TWENTY", value: 20.0 },
{ name: "TEN", value: 10.0 },
{ name: "FIVE", value: 5.0 },
{ name: "ONE", value: 1.0 },
{ name: "QUARTER", value: 0.25 },
{ name: "DIME", value: 0.1 },
{ name: "NICKEL", value: 0.05 },
{ name: "PENNY", value: 0.01 }
];

function checkCashRegister(price, cash, cid) {
let change = cash - price;

let totalCid = cid.reduce((acc, next) => {
  return acc + next[1];
}, 0.0)

console.log('totalCid:',totalCid)

if(totalCid < change) {
  return {status: "INSUFFICIENT_FUNDS", change: []}
} else if(totalCid === change) {
  return {status: "CLOSED", change: [...cid]}
}

cid = cid.reverse();

let result = denom.reduce((acc, next, index) => {
  if(change >= next.value) {
    let currentValue = 0.0;
    while (change >= next.value && cid[index][1] >= next.value) {
      currentValue += next.value;
      change -= next.value;
      change = Math.round(change * 100) / 100; // Avoid rounding errors
      cid[index][1] -= next.value;
    }
    acc.push([next.name, currentValue]);
    return acc
  } else {
    return acc
  }
}, [])
console.log(result)
return result.length > 0 && change === 0 ? result : {status: "INSUFFICIENT_FUNDS", change: []};
}

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

// Expected result: {status: "OPEN", change: [["QUARTER", 0.5]]}.

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

// Expected result: {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}

Your browser information:

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

Challenge: Cash Register

Link to the challenge:

Notice that both of these are for case when status is OPEN. Take a closer at line returning that case.

1 Like

Thank you so much for helping me figuring out the final answer:


const denom = [
  { name: "ONE HUNDRED", value: 100.0 },
  { name: "TWENTY", value: 20.0 },
  { name: "TEN", value: 10.0 },
  { name: "FIVE", value: 5.0 },
  { name: "ONE", value: 1.0 },
  { name: "QUARTER", value: 0.25 },
  { name: "DIME", value: 0.1 },
  { name: "NICKEL", value: 0.05 },
  { name: "PENNY", value: 0.01 }
];

function checkCashRegister(price, cash, cid) {
  let change = cash - price;

  let totalCid = cid.reduce((acc, next) => {
    return acc + next[1];
  }, 0.0)

  console.log('totalCid:',totalCid)

  if(totalCid < change) {
    return {status: "INSUFFICIENT_FUNDS", change: []}
  } else if(totalCid === change) {
    return {status: "CLOSED", change: [...cid]}
  }

  cid = cid.reverse();

  let result = denom.reduce((acc, next, index) => {
    if(change >= next.value) {
      let currentValue = 0.0;
      while (change >= next.value && cid[index][1] >= next.value) {
        currentValue += next.value;
        change -= next.value;
        change = Math.round(change * 100) / 100; // Avoid rounding errors
        cid[index][1] -= next.value;
      }
      acc.push([next.name, currentValue]);
      return acc
    } else {
      return acc
    }
  }, [])
  console.log(result)
  return result.length > 0 && change === 0 ? { status: 'OPEN', change: result } : {status: "INSUFFICIENT_FUNDS", change: []};
}