JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Hi there!
Any idea why the tests do not recognize that Im returning an object?
All outputs seem to be right to be, but all are recognized as wrong by the tests…

Your code so far

function checkCashRegister(price, cash, cid) {

    // create changeInfo object
    let CHANGEINFO = {};
    CHANGEINFO['PENNY'] = 0.01;
    CHANGEINFO['NICKEL'] = 0.05;
    CHANGEINFO['DIME'] = 0.1;
    CHANGEINFO['QUARTER'] = 0.25;
    CHANGEINFO['ONE'] = 1;
    CHANGEINFO['FIVE'] = 5;
    CHANGEINFO['TEN'] = 10;
    CHANGEINFO['TWENTY'] = 20;
    CHANGEINFO['ONE HUNDRED'] = 100;

    // create changeWeights object
    let CHANGEAMOUNTPERUNIT = {};
    function changeObjBuilder(cid, obj) {
        for (var i = 0; i < cid.length; i++) {
            cid.forEach(valuta => {
                obj[String(valuta[0])] = valuta[1];
            });
        }
        return obj;
    }
    CHANGEAMOUNTPERUNIT = changeObjBuilder(cid, CHANGEAMOUNTPERUNIT);

    // create array with only the names of the money-units
    let unitNamesArray = Object.keys(CHANGEINFO);
    // create array with only the values of the money-units
    let valueOfUnitsArray = Object.values(CHANGEINFO);
    // create array with only the weights
    let amountOfEachUnitArray = Object.values(CHANGEAMOUNTPERUNIT);

    // calculate change that has to be returned
    let totalChange = cash - price;
    // define change array
    let change = [];

    // define change object and return it in the end
    changeObj = {};

    // if all money is gone, close the thing
    // first calculate totalFunds
    let totalFunds = 0;
    amountOfEachUnitArray.forEach(element => {
        totalFunds += element;
    });
    if (totalChange == totalFunds) {
        changeObj.status = "CLOSED";
    }

    // check values from biggest to lowest and loop until complete change has calculated
    for (var i = valueOfUnitsArray.length - 1; i >= 0; i--) {

        // in case all money will be gone, a lot of zero arrays need to be added
        if (changeObj.status == "CLOSED") {
            let unitValue = valueOfUnitsArray[i];
            let changeName = unitNamesArray[i];
            let restValue = amountOfEachUnitArray[i];
            // define subarray to push to change array
            change.push([changeName, restValue]);
        }
        else {
            // check if value is smaller than changevalue
            if (totalChange >= valueOfUnitsArray[i]) {
                let unitValue = valueOfUnitsArray[i];
                let changeName = unitNamesArray[i];
                let restValue = amountOfEachUnitArray[i];

                if (restValue == 0) {
                    continue;
                }

                if (totalChange - restValue >= 0) {
                    totalChange = totalChange - restValue;
                    totalChange = totalChange.toFixed(2);
                    // define subarray to push to change array
                    change.push([changeName, restValue]);
                }
                else {
                    // Modulus: need to calculate how often it fits in... if Five 55 needs to only give 15: need to find out how to get as close to 0 as possible
                    let roundedNumber = Math.floor(totalChange / unitValue); // this would then give 3
                    restValue = roundedNumber * unitValue;
                    totalChange = totalChange - restValue;
                    totalChange = totalChange.toFixed(2);
                    // define subarray to push to change array
                    change.push([changeName, restValue]);
                }
            }
        }
    }

    if (totalChange != 0 && changeObj.status != "CLOSED") {
        // if there is still totalChange left: INSUFFICIENT FUNDS?
        changeObj.status = "INSUFFICIENT_FUNDS";
        changeObj.change = [];
    }
    else {
        if (changeObj.status != "CLOSED"){
            changeObj.status = "OPEN";
            changeObj.change = change;
        }
        changeObj.change = change;
    }



    console.log(changeObj);
    return changeObj;
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:104.0) Gecko/20100101 Firefox/104.0

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

where is the keyword for declaring this?

1 Like

when tests fail, the first thing you should do is take the first failing test, add it in your editor, and console.log it

it says exactly what’s the issue
image

1 Like