Cashier register challenge

This is my first thread/comment here, so hello everybody :),

I’m currently working on the Cash Register challenge but got stuck really badly. Hope you guys can help me out. First of all here is my code:

function checkCashRegister(price, cash, cid) {
    let change = parseFloat((Number(cash - price).toFixed(2)));
    let outputChange = [];
    var moneyInDrawer = 0;
    var copyCid = JSON.parse(JSON.stringify(cid));

    // Creating object for the output
    result = {
        status : "",
        change : []
    }

    //Summing up the money in the drawer
    for (let x = 0; x < copyCid.length; x++) {
        moneyInDrawer += copyCid[x][1];
    }

    moneyInDrawer = Number(moneyInDrawer.toFixed(2));

    //Map to compare value
    const unitAmountMap = {
      "PENNY" : 0.01,
      "NICKEL" : 0.05,
      "DIME" : 0.1,
      "QUARTER" : 0.25,
      "ONE" : 1.00,
      "FIVE" : 5.00,
      "TEN" : 10.00,
      "TWENTY" : 20.00,
      "ONE HUNDRED" : 100.00
    }

    for (i = 8; i >= 0; i--) {
      var helperArr = []; //Subarray ["UNIT", amount]
      var amount = 0;
      helperArr.push(copyCid[i][0]); //Adding UNIT to subarray

      while (change - unitAmountMap[copyCid[i][0]] >= 0 && copyCid[i][1]) { //Calculating amount per unit
        change = parseFloat(Number(change - unitAmountMap[copyCid[i][0]]).toFixed(2));
          
        amount = parseFloat(Number(amount + unitAmountMap[copyCid[i][0]]).toFixed(2));

        moneyInDrawer = parseFloat(Number(moneyInDrawer - unitAmountMap[copyCid[i][0]]).toFixed(2));

        copyCid[i][1] = parseFloat(Number(copyCid[i][1] - unitAmountMap[copyCid[i][0]]).toFixed(2));
      }
        
        helperArr.push(amount);
        if (helperArr[1] > 0) {
        outputChange.push(helperArr);
        }
    }

    if (change > 0) {
        result.status = "INSUFFICIENT_FUNDS";
        return result;
        }
    else if (change == 0 && moneyInDrawer == 0) {
        result.status = "CLOSED";
        result.change = cid;
        return result;
        }
    else if (change == 0 && moneyInDrawer > 0) {
        result.status = "OPEN";
        result.change = outputChange;
        console.log(typeof(result))
        return result;
        }
}

I ran all the test cases in the dev mode in my browser and think the outputs are correct. Anyway, none of the tests passes. After debugging and browsing through the forum for hours I am quite desperate :sweat_smile:

Obviously, I am not delivering the required object, although typeof(result) shows the correct return type.

I’m very grateful for your answers.

Gabriel

Hey, welcome to the forum.

When you post on a challenge, please include a link to the challenge: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register

Putting in your code, I see this error in the FCC console:

ReferenceError: result is not defined

You never declare that. When I declare that, that error goes away and the same error appears for a different variable. When I fix that, all the tests pass for me.

Always declare your variables. In a non-strict JS env you can get away with that, but it is bad practice.

2 Likes

Hi Kevin,

sry, for not posting the required link to the challenge. Will do this in the future. And thank you so much for your help. Everything runs smoothly now :slight_smile: Both thumbs up!

1 Like

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