Https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/

Https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/
0

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/

Hi,

I’m doing the last challenge for the FCC JS certification, the cash register. I’m facing a bug and I’m not sure it’s due to my code… I keep receiving an ‘assignment to undeclared variable element’ in the FCC console after running the tests, but I can’t reproduce this in the debugger, my editor or browser console and my code is working: producing the output I’m expecting.

The thing is that potential bug (using ‘potential’ here, because it still can be a mistake of mine and not a bug… ^^) is preventing me to finish the challenge, because next to this issue, I have a last thing to fix in my logic, but I can’t check using FCC tests (I wanted to see which ones I pass and which ones I fail to help me fix the conditional logic…)

I’d be happy if someone could have a look, although I don’t know where to put the snippet, because I don’t want to be a spoiler… :slight_smile:

We need to see your code in order to help you.

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

So first of all, I’m not done with the part where I check for ‘unsufficient_funds’, ‘closed’, or ‘open’: I know the code is executing and counting the change to give back even after I stated that there isn’t actually enough funds in the cash register… I’ll fix it! :wink:
So here it’s only about that ‘assignment to undeclared variable element’ in FCC console that I’m wondering… :slight_smile:

While formatting the code for the forum I see it might come from the ‘checkForFunds’ variable, although I don’t understand why…

Oh, it’s not easier to read with Polacode, sorry… Here it is:

// 1. Calculate how much money is in the cash register
const calcAmountInRegister = array => {
    let amount = 0;

    for (element of array) {
        amount += element[1];
    }

    return Number(amount.toFixed(2));
};

// 2. Calculate the amount to give back
const calcGiveBack = (price, cash) => {
    let giveBack = cash - price;

    return Number(giveBack.toFixed(2));
};

// 3. Check for INSUFFICIENT_FUNDS case
const isThereEnough = (amountIn, amountOut) => {
    let notEnough;

    notEnough =
        amountIn - amountOut < 0
            ? {
                  status: 'INSUFFICIENT_FUNDS',
                  change: [],
              }
            : 'Indeed, there is enough!';

    return notEnough;
};

// 4. Add the value of each bill and coin to the passed array
const addValueToCid = (cid, value) => {

    for (let i = 0; i < value.length; i++) {
        cid[i].push(value[i]);
    }

    return cid;
};

// 5. Calculate the number of bills or coins for each currency unit
const howMuchPerCurrencyUnit = array => {
    for (element of array) {
        element.push(Number((element[1] / element[2]).toFixed(2)));
    }
    return array;
};

const changeFunction = (back, array) => {
    let change = {
        status: 'OPEN',
        change: [],
    };

    for (let i = array.length - 1; i >= 0; i--) {

        if (back >= array[i][2]) {
            let sum = 0;

            while (back >= array[i][2] && array[i][3] !== 0) {
                sum += array[i][2];

                array[i][3] -= 1;

                back = (back - array[i][2]).toFixed(2);
                
                back = Number(back);
            }

            change.change.push([array[i][0], sum]);
        } else {
            continue;
        }
        back = Number(back.toFixed(2));
    }
    return change;
};

function checkCashRegister(price, cash, cid) {
    let amountInRegister, giveBack, checkForFunds, newCid, change;
    // 1. Store the value of each bill and coin
    const valueOfEach = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];

    amountInRegister = calcAmountInRegister(cid);

    giveBack = calcGiveBack(price, cash);

    checkForFunds = isThereEnough(amountInRegister, giveBack);

    newCid = addValueToCid(cid, valueOfEach);

    newCid = howMuchPerCurrencyUnit(newCid);

    change = changeFunction(giveBack, newCid);
    return change;
}

You didn’t declare element. freeCodeCamp uses a strict mode that requires variables to be declared before they are assigned.

Faster than speed light!! Thanks a lot !

Um… How do I declare a variable in that kind of loop? :thinking:
Would you have an example?

The same way you do in a normal for loop: just use the appropriate keyword.

for(let thing of stuff) {
    doSome(thing);
}

Thank you for your help :+1:

I’m glad I could help. Happy coding!