Noob Coder looking for Pros advice

The following is the code i’ve used to pass The Last Project in JavaScript Algorithms and Data Structures Projects, Please any suggestions on how to improve that code, i know it is too messy and needs improvements, constructive criticism or advice are very welcomed, Thank you for your time <3

function checkCashRegister(price, cash, cid) {
  
  /*Functions START*/
  const floatMe = x => parseFloat(x.toFixed(2));
  //^^^function to reduce floating point to 2
  const sumArr = arr => floatMe(arr.reduce((acc, item) => acc + item));
  //^^^function that sums elements of an array
  /******** */
  /*↓↓Function that copies an array of 2 dimensions↓↓ */
  function copyMe2(original) {
  let copy = [];
  for (let i of original) {
    let tempArr = []
    for (let j of i) {
      tempArr.push(j);
    }
    copy.push(tempArr);
    }
    return copy;
  }
  /*Functions END*/
  /* * * * * * * * * * */
  /*global vars START*/
  const change = floatMe(cash - price);
  const sumCid = cid.reduce(((acc, item)=> floatMe(acc + item[1])), 0);
  //^^^Sum of the cash in the cid
  const insufficient = {status: "INSUFFICIENT_FUNDS", change: []};
  const closed = {status: "CLOSED", change: cid};
  const currency = {
    "ONE HUNDRED" : 100,
    "TWENTY" : 20,
    "TEN" : 10,
    "FIVE" : 5,
    "ONE" : 1,
    "QUARTER" : 0.25,
    "DIME" : 0.1,
    "NICKEL" : 0.05,
    "PENNY" : 0.01
  }
  /*global vars END*/

  if (change > sumCid) {//1st case
    return insufficient;
  } else if (change === sumCid) {//2nd case
    return closed;
  } else { ////////////////↓3rd CASE↓////////////////
    let cId = copyMe2(cid).reverse().map((item) => Array(Math.floor(item[1]/currency[item[0]])).fill(currency[item[0]]));
    /*^^^ make an array filled with denominations, for example if i have
    [300] in the hundreds, it will become [100,100,100]  */
    let howMany = [];
    let changeCopy = change;//to make a copy of the "change" variable

    for (let i of cId) {
      let sum = 0;
      for (let j of i) {
        if (floatMe(changeCopy - j) >= 0) {
          changeCopy -= j;
          sum += j;
        } else {
          break;
        }
      }
      howMany.push(sum);
    }

    return (sumArr(howMany) !== floatMe(change))
    ? {status: "INSUFFICIENT_FUNDS", change: []}//If there is not enough change
    : (() => { 
    let lC = 0
    let finalArr = []
    for (let i in currency) {
      if (howMany[lC] > 0) {
        finalArr.push([i, howMany[lC]])
      }
      lC++;
    }
    return {status: "OPEN", change: finalArr};
    })()
  }
}

I’ve edited your post.

If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like

sorry i forgot to hide the solution

I didn’t really look at the code logic but some of your function and variable names should be more descriptive about what they do/contains.

copyMe2

That name doesn’t really tell me much, sure you have “copy” in the name but other than that it isn’t really clear what it does.

sumArr

Sum of what? Also, the type isn’t really as useful as a clear name.

const nameArr = ['Jack', 'Jill']

const usersList = ['Jack', 'Jill']
1 Like

Thank you for your reply :smile: