[JavaScript] Cash Register challenge

Tell us what’s happening:
I would appreciate any sort of feedback on the code I came up with down below. Tell me if there is anything I could’ve done better, ranging from complexity to the tidiness of the code. Thanks in advance!

Your code so far

function checkCashRegister(price, cash, cid) {
  let total= 0;
// I multiplied everything by 100 to work around the issues that come with JS concerning floats.
  let values= [["PENNY", 1],["NICKEL", 5],["DIME", 10],["QUARTER", 25],["ONE", 100],["FIVE", 500],["TEN", 1000],["TWENTY", 2000],["ONE HUNDRED", 10000]].reverse();
// availCash is just cid, but with all its values multiplied by 100 again.
  let availCash = [...cid].reverse().map(el => [el[0], el[1] * 100]);
// The code down below figures out how much money the register contains in total!
  for(let i=0;i<cid.length;i++){
    total+=(availCash[i][1]);
  }
  let valueToReturn= (cash *100-price *100);


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


  if(valueToReturn==total){
    return {status: "CLOSED", change: cid};
  }

// The while loop basically builds the change array if the past two tests fail.
  let i=0;
  let j=0;
// The variable a represents the object to return in the "OPEN" case. 
  let a={status: "OPEN", change: []};
  while(valueToReturn>=0 && i<availCash.length){
    if(valueToReturn<values[i][1] || availCash[i][1]==0){
      i++;
      j=0;
    } else {
      if(j==0){
          a["change"].push([values[i][0],values[i][1]/100]);
          j++;
          availCash[i][1]=availCash[i][1]-values[i][1];
      } else {
          a["change"][a["change"].length-1][1]+=values[i][1]/100;
          availCash[i][1]=availCash[i][1]-values[i][1];
      }
      valueToReturn=valueToReturn-values[i][1];
    }


// The code down below ensures the notes available can be added up to the valueToReturn. If not, valueToReturn wouldn't be 0 even after the while loop.
  }
  if(valueToReturn!=0){
    return {status: "INSUFFICIENT_FUNDS", change: []};
  } else {
    return a;
  }
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.115 Safari/537.36 OPR/88.0.4412.85

Challenge: Basic JavaScript Algorithms and Data Structures Projects -Cash Register

Link to the challenge:

I haven’t really looked into the code, but you can improve the readability of the code, for example as posted below.

Readable code is easily maintainable even when its complex or of sub-standard. Others reading it for maintenance, enhancement or study will appreciate it when presented well. This includes appropriate comments, spacing between keywords and operators and variables, indentation, empty lines between groups of statements.

function checkCashRegister(price, cash, cid) {

    let total = 0;

    // I multiplied everything by 100 to work around the issues that come with JS 
    // concerning floats.
    let values = [
        ["PENNY", 1], ["NICKEL", 5],
        ["DIME", 10], ["QUARTER", 25],
        ["ONE", 100], ["FIVE", 500],
        ["TEN", 1000], ["TWENTY", 2000],
        ["ONE HUNDRED", 10000]
    ].reverse();

    // availCash is just cid, but with all its values multiplied by 100 again.
    let availCash = [...cid].reverse().map(el => [ el[0], el[1] * 100 ]);

    // The code down below figures out how much money the register contains in total!
    for (let i = 0; i < cid.length; i++) {
        total += availCash[i][1];
    }

    let valueToReturn = (cash * 100 - price * 100);

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

    if (valueToReturn == total) {
        return { status: "CLOSED", change: cid };
    }

    let i = 0;
    let j = 0;

    // The variable a represents the object to return in the "OPEN" case. 
    let a = { status: "OPEN", change: [] };

    // The while loop basically builds the change array if the past two tests fail.
    while (valueToReturn >= 0 && i < availCash.length) {

        if (valueToReturn < values[i][1] || availCash[i][1] == 0) {
            i++;
            j = 0;
        } else {
            if (j == 0) {
                a["change"].push([values[i][0], values[i][1] / 100]);
                j++;
                availCash[i][1] = availCash[i][1] - values[i][1];
            } else {
                a["change"][a["change"].length - 1][1] += values[i][1] / 100;
                availCash[i][1] = availCash[i][1] - values[i][1];
            }
            valueToReturn = valueToReturn - values[i][1];
        }
        // The code down below ensures the notes available can be added up to the valueToReturn. 
        // If not, valueToReturn wouldn't be 0 even after the while loop.
    }

    if (valueToReturn != 0) {
        return { status: "INSUFFICIENT_FUNDS", change: [] };
    } else {
        return a;
    }
}
1 Like

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