Cash Register JS Algorithms and Data Structures

Tell us what’s happening:
Describe your issue in detail here.
My code works on VS Code and passes all the tests yet it fails tests two and three when I enter the code on FCC editor.
I have checked the variables for correct spelling and also the status texts.

**Your code so far**
// array of denominations and values

var denoms = [

    ["ONE HUNDRED", 100.0],

    ["TWENTY", 20.0],

    ["TEN", 10.0],

    ["FIVE", 5.0],

    ["ONE", 1.0],

    ["QUARTER", 0.25],

    ["DIME", 0.1],

    ["NICKEL", 0.05],

    ["PENNY", 0.01]

];

function checkCashRegister(price, cash, cid) {

    let output = { status: null, change: [] }

    let changeArr = [];

    let change = cash - price;

    // get total amount in cid

    let regTotal = cid.reduce((acc, current) => acc + current[1], 0);

    // console.log(regTotal);

    // when result is exact change

    if (regTotal == change) {

        output.status = "CLOSED";

        output.change = cid;

        return output;

    }

    // when there is insufficient funds

    if (regTotal < change) {

        output.status = "INSUFFICIENT_FUNDS";

        return output;

    }    

    let register = cid.reverse();

    let value = null;

    for (let i = 0; i < denoms.length; i++) {

        value = 0;

        if (denoms[i][1] > change) {

            // get next lower currency denomination

            continue;

        } else {

            while (register[i][1] > 0 && change >= denoms[i][1]) {

                change -= denoms[i][1];

                register[i][1] -= denoms[i][1];

                value += denoms[i][1];

                change = Math.round(change * 100) / 100;

            }

        }        

        // if this denom was used then add to changeArr

        if (value > 0) {

            changeArr.push(denoms[i][0], value);

        }

    }

    // If there are no elements in change_arr or we have leftover

    // change, return the string "Insufficient Funds"

    if (changeArr.length < 1 || change > 0) {

        output.status = "INSUFFICIENT_FUNDS";

        return output;

    }

    // transaction good, change and status returned

    output.status = "OPEN";

    output.change = changeArr;

    return output;

};
**Your browser information:**

Google Chrome : Version 90.0.4430.212 (Official Build) (64-bit)
and VS Code with node.js
Both run my code passing all the tests

Challenge: Cash Register

Link to the challenge:

Can you say which tests fail?

You are using a global variable, which I am suspicious of.

1 Like

The change property of your output is supposed to be an array of arrays. Your output.change property is a single array. That’s the main reason your tests aren’t passing.

Thanks for your help. I had the denoms array inside my original code and just tried moving it global to see if there was any difference. I have since put it back as a local variable in the main function.
The problem was pointed out to me by DrantDumani that I was returned a one dimensional array instead of a 2D array which I have since changed and now code passes all the tests.

1 Like

Thanks for your help. A simple code change to 2D array solved the problem. My code now passes all of the tests.

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