Challenge: Cash Register Solution (review needed)

hi,
I have completed the final project JavaScript Algorithms and Data Structures: Cash Register without reduce() method. Using some if else and a pinch of while loop.
Its documented too, within the limitations of my understanding.
Advice me towards further improvements.
Regards.

function checkCashRegister(price, cash, cid) {
    let change = cash - price;  //Get the change
    cid = cid.reverse();  //Reverse the given 'cid'. All the arrays containing denominations should be in same order (ascending/descending) so that each index value relates to the same denomination in every array.
    let value = 0;    //For the change(key). values in sub-arrays of resulting array.
    let resultArr = [];  //Resulting array. change(key).
    let resultObj = {status: "", change: []};  //Resulting Object to modify according to the outcomes.
    let cidAdd = 0;  //To add all the values in 'cid' to see if change is less than or equal to available cash in 'cid'.
    
    //List of all denominations with their respective values in descending order.
    let denomination = [["ONE HUNDRED", 100], ["TWENTY", 20], ["TEN", 10],["FIVE", 5], ["ONE", 1], ["QUARTER", 0.25], ["DIME", 0.1], ["NICKEL", 0.05], ["PENNY", 0.01]];
    for (let i = 0; i < cid.length; i++){             
       cidAdd += cid[i][1];
    } if (change > cidAdd){
       resultObj.status = "INSUFFICIENT_FUNDS";     //Insufficient funds case.
       return resultObj;
    } else if (change == cidAdd) {
       resultObj.status = "CLOSED";      //Apparently drawer can not be left absolutely cashless.
       resultObj.change = cid.reverse();
       return resultObj;
    } else {
       for (let i = 0; i < denomination.length; i++){      //The open case.
        let denomArr = [];
        if (denomination[i][1] <= change){
           
           while ( cid[i][1] >= denomination[i][1] && denomination[i][1] <= change){
              value += denomination[i][1];
              change -= denomination[i][1];
              cid[i][1] -= denomination[i][1];      //Also updates the 'cid' though not required by the challenge but doing so seemed fair and felt good.
            change = Math.round(change * 100) / 100;  //Only needed when subtracting.
              cid[i][1] = Math.round(cid[i][1] * 100) / 100;
           } denomArr.push(cid[i][0]);
           denomArr.push(value);
            resultArr.push(denomArr);
           value = 0;
        }  
     }
     //At this point all the change should have been spread into denominations. If not then as per the conditions defined to perform the task indicate that there exists a higher denomination but can not be utilized as in the case where change=0.5 and we have "ONE"=1 but can not use it.
     if (change !== 0){
        resultObj.status = "INSUFFICIENT_FUNDS";
        return resultObj;
     } else {
       resultObj.status = "OPEN";
     resultObj.change = resultArr;
     return resultObj;
     }
    }
 }

FYI, the codepen link you gave goes to the codepen home page, not your personal pen for this solution.

send me a private message or reply here. I`ll send you the link to the solution.
should not post the solution here.

Hi @lethemeer !
Welcome to the forum!

You can post your solution as long as you wrap them in spoiler tags.
Since you are looking for a code review, it is ok to post your solution as long as it is in those spoiler tags.

Screen Shot 2021-07-06 at 2.56.42 PM

The only time is is not ok to post solutions is if you are giving it out to someone else that needs help with a challenge.

Hope that is clear.

ok. thx for helping.

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