JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Describe your issue in detail here.
I don’t know what is exatly the problem

const currencyUnit ={
  "PENNY":1,
  "NICKEL":5,
  "DIME" :10,
  "QUARTER":25,
  "ONE" : 100,
  "FIVE" :500,
  "TEN" :1000,
  "TWENTY" :2000,
  "ONE HUNDERED" :10000
}

function checkCashRegister(price, cash, cid) {
  let changeSum = cash*100-price*100;
  let changeSumCheck=changeSum;
  let change=[];
  let status= '';


  let cidSum = 0;
  let filteredCid =cid.filter(elem=> elem[1] !==0).reverse();
  filteredCid.forEach(elem => {
    let curr = elem[0];
    let currSum =elem[1] *100;
    cidSum += currSum;
    let amount =0;
    
    while (changeSum >= currencyUnit[curr]&& currSum >0) {
      amount += currencyUnit[curr];
      changeSum -= currencyUnit[curr];
    

    }
    if(amount !== 0) {
      change.push([curr,amount/100]);
    }
  });
  if (changeSum > 0) {
    status = 'INSUFFICIENT_FUNDS';
    change= [];
  } else if ( changeSum ==0 && changeSumCheck == cidSum) {
    status='CLOSED';
    change =cid;
  } else {
    status = 'OPEN';
  }
  return { 'status':status,'change':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]]);

Your code so far

const currencyUnit ={
  "PENNY":1,
  "NICKEL":5,
  "DIME" :10,
  "QUARTER":25,
  "ONE" : 100,
  "FIVE" :500,
  "TEN" :1000,
  "TWENTY" :2000,
  "ONE HUNDERED" :10000
}

function checkCashRegister(price, cash, cid) {
  let changeSum = cash*100-price*100;
  let changeSumCheck=changeSum;
  let change=[];
  let status= '';


  let cidSum = 0;
  let filteredCid =cid.filter(elem=> elem[1] !==0).reverse();
  filteredCid.forEach(elem => {
    let curr = elem[0];
    let currSum =elem[1] *100;
    cidSum += currSum;
    let amount =0;
    
    while (changeSum >= currencyUnit[curr]&& currSum >0) {
      amount += currencyUnit[curr];
      changeSum -= currencyUnit[curr];
    

    }
    if(amount !== 0) {
      change.push([curr,amount/100]);
    }
  });
  if (changeSum > 0) {
    status = 'INSUFFICIENT_FUNDS';
    change= [];
  } else if ( changeSum ==0 && changeSumCheck == cidSum) {
    status='CLOSED';
    change =cid;
  } else {
    status = 'OPEN';
  }
  return { 'status':status,'change':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]]);

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

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

Hello. I didn’t do a deep look, but picked this failed test to focus on:

checkCashRegister(19.5, 20, [["PENNY", 0.01]])
should return: {status: "INSUFFICIENT_FUNDS", change: []}.

Yours returns:
{ status: 'OPEN', change: [ [ 'PENNY', 0.5 ] ] }

So for that example, you have one penny in the bank, so it should be insufficient to return the 5 cents due… but you’re says it returned 5 cents, and then said there was still money left in the bank.

Looking at your code, I’m wondering if this loop is the problem. It seems currSum contains the amount of the coin you’re on, in this case pennys… so it does check to make sure you have some pennys, but through the loop, it doesn’t update how many pennys you have left… so you started out with 1 penny, but as you don’t update the count in the bank as you go, currSum stays at 1 penny, allowing you to complete this loop 5 times, and still leave 1 penny in the bank.

    while (changeSum >= currencyUnit[curr] && currSum > 0) {
      amount += currencyUnit[curr];
      changeSum -= currencyUnit[curr];

Not sure if that’s causing any problems with the other test cases, but that’s definitely a problem. Sometimes its useful to focus on a single test case. You can change the line of code at the end to:

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

That way you can see in your console log the result of this test, so you can more easily troubleshoot. Then for other failed test cases, you can again update that line for the other test case.