JavaScript Algorithms and Data Structures Projects - Cash Register : My algorithm produces the same output as test cases but one test is not passing

Tell us what’s happening:
My algorithm gives the same output for all the test cases, even for the failling one.

Your code so far

function checkCashRegister(price, cash, cid) {
  const currencies = [
        0.01,
        0.05,
        0.1,
        0.25,
        1,
        5,
        10,
        20,
        100
      ];

  let change = cash - price;
  let changeArr = []

  let total = cid.reduce((sum, item) => sum += item[1], 0)
  let status = total > change? "OPEN" : total === change ? "CLOSED" : "INSUFFICIENT_FUNDS";
  
  if(status === "INSUFFICIENT_FUNDS"){
    return {status: "INSUFFICIENT_FUNDS", change: []}
  }

  if(status === "CLOSED")
  {

      return {
      status : status,
      change: cid
    }
  }


  
  for(let i=8; i>=0; i--)
  {

    let x = 0;
    while(change >= currencies[i] && cid[i][1] > 0)
    {
      x += currencies[i];
      change -= currencies[i];
      cid[i][1] -= currencies[i];

      change = Math.round(change*100)/100;
    }

    if(x > 0)
    {
      changeArr.push([cid[i][0], x]);
    }
  }

  return {
    status : "OPEN",
    change: changeArr
  }
}


console.log(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

But { status: 'OPEN', change: [ [ 'PENNY', 0.01 ] ] } is not the same as {status: "INSUFFICIENT_FUNDS", change: []} for the failing test case.

Thank you Jeremy! It seems that I accidentally copied the test input above the failing one and was wondering why it wasn’t passing. I just realized that I was missing an edge case where the total is greater than the change, but we don’t have the required currencies. I handled that edge case, and now I have obtained my certification.

thanks again

1 Like

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