Feedback for final Javascript Project

Finished the final javascript project, I got a lot of good feedback on my last project so I thought I’d post this one to see if you guys have any advice or thoughts for me.

function checkCashRegister(price, cash, cid) {

//**create a function that takes our cid minus our cash and return if it's positve(true), negative(false), or equal(undefined)*/
  var check = function(cid,cash) {
  let result = []
  let subtract = cash - price;
  cid.forEach(x => result.push(x[1]))
  result = result.reduce((a,b) => a+b, 0) * 100 / 100;
    if(result - subtract === 0) {
    return undefined;
  } else if(result - subtract > 0) {
    return true;
  } else return false;
};

//**create our object to store our results */
let returned = {status:"", change: []};

//**if check was false return object as follows with the array unchanged */
if(check(cid, cash) == false) {
    returned.status = "INSUFFICIENT_FUNDS";
    console.log(returned)
    return returned;
//**if check was undefined return closed with cid as our change */    
} else if(check(cid, cash) == undefined) {
    returned.status = "CLOSED"
    returned.change = cid;
    console.log(returned)
    return returned;
//**if check was true we take a new array with the values of the change;; create our change and store it in hold variable;; initialize for loop to run through the cid;; if cid is greater than 0 and hold minus the cid[i] is greater than 0 we;; minus the change amount from our change array from hold. math.round was required to keep the value from going down by 0.000001. then we minus the cid by our array change[i]. array stores our change name for when we return and place stores the value. while our condition remains true we continue to add to place and minus from our hold and cid. we then reduce place to a single value. rest is the place and array values put together. finally we push this array into the last array to create our seperated arrays in the result. */    
} else if(check(cid, cash) == true) {
    let change = [["PENNY",0.01], ["NICKEL",0.05], ['DIME',0.1], ['QUARTER',0.25], ["ONE",1], ['FIVE',5], ['TEN',10.00], ['TWENTY',20.00], ['ONE HUNDRED',100.00]];
    let hold = (cash - price);
    let array;
    let place;
    let rest;
    let last = [];

    for(let i = cid.length - 1; i >= 0; i--) {
        if(cid[i][1] > 0 && hold - change[i][1] >= 0) {
         hold -= change[i][1];
         hold  = Math.round(hold*100)/100;
         cid[i][1] -= change[i][1];
         array = [change[i][0]];
         place = [change[i][1]];
        while(cid[i][1] > 0 && hold - change[i][1] >= 0) {   
           hold -= change[i][1];
           hold = Math.round(hold * 100)/100;
           cid[i][1] -= change[i][1];
           place.push(change[i][1]);
      }; 
        place = place.reduce((a,b) => a+b,0);
        rest = array.concat(place);
        last.push(rest);
        };
    };

  //**if hold has been reduced to zero because there was enough change, return open and with last as our completed change array;; otherwise insufficient funds because there wasn't enough change in our drawer */
        if(hold == 0) {
          returned.status = "OPEN";
          returned.change = last;
          console.log(returned)
          return returned;
        } 
          else {
          returned.status = "INSUFFICIENT_FUNDS"
          returned.change = [];
          console.log(returned)
          return returned;
        };
       

  };
};

checkCashRegister(60, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])

Pretty happy that I finished this one without looking at the solution. This is also just how I finally passed it so I’m sure there’s refactoring or ways I could have shortened things that I haven’t seen yet. Thanks in advance for any advice or comments :slightly_smiling_face:

Hi,

Couple of feedback:

  1. In your if condition it is better to use ===
  2. I will refactor the check function to return string. Returning false, undefined and true kind of tricky, since undefined and false is both falsy value.
  3. you can write else {} instead of else if (check(...) == true), because there is no else after it.
1 Like

well, there’s a lot of code, and not simple code but rather a complex one, you can do it in a lot less code and even less actions, iterative approach is best (you can use while loops like i did)

1 Like