Cash Register HELP!

Hey All!

I think I have finally got the Cash Register project for freecodecamp working.
But the only problem is that for some reason the freecodecamp tests arent accepting it?
Even though to me it looks like the results are all correct.

I would really appreciate anyone who could check this out with me, and let me know where im going wrong,

html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Cash Register</title>
    </head>

    <body>
        <h1>Cash Register</h1>
        <input id = "cash">
        <div id = "change-due"></div>
        <button id = "purchase-btn" onclick = "press()">purchase</button>

        <div id = "register">Register</div>

        <script src = "script.js"></script>

    </body>

</html>

javascript:

let price = 19.5;
let cash = document.getElementById("cash");
let changeDue = document.getElementById("change-due");
let cashValue = parseFloat(cash.value);


let cid = [
  ["PENNY", 0.5],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 0],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0]
];

let totalStock = (cid[0][1] + cid[1][1] + cid[2][1] + cid[3][1] + cid[4][1] + cid[5][1] + cid[6][1] + cid[7][1] + cid[8][1]);

let reg = {
    0.01: cid[0][1],
    0.05: cid[1][1],
    0.1: cid[2][1],
    0.25: cid[3][1],
    1: cid[4][1],
    5: cid[5][1],
    10: cid[6][1],
    20: cid[7][1],
    100: cid[8][1],        
}

function press() {
    let cashValue = parseFloat(cash.value);
    let change = parseFloat(cashValue - price);

    if (cashValue < price || cash.value.trim() === "") {
        alert("Customer does not have enough money to purchase the item");
    } else if (cashValue === price) {
        changeDue.innerHTML = ("No change due - customer paid with exact cash");
    } else if (totalStock < change) {
        console.log("Total Stock: " + totalStock)
        console.log("Price: " + price)
        changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS";
    } else {
        calc();
        console.log("total stock: " + totalStock)
        console.log("change: " + change)
    }
}


function calc() {
    let cashValue = parseFloat(cash.value);
    let uneditedChange = parseFloat(cashValue - price);
    let change = parseFloat(cashValue - price);
    console.log("cash: " + cashValue + " - " + "price: " + price + " = change: " + uneditedChange.toFixed(2))

    const stock = Object.keys(reg).sort(function(a, b) {
        return a - b;
    });

    stock.reverse();

    stock.forEach(key => {
        
                      

        while (change.toFixed(2) >= parseFloat(key) && reg[key] > 0) {
            change -= key;
            console.log("Change: " + change.toFixed(2) + " Key " + key + " reg: " + reg[key])
            reg[key] -= key;
            

            if (parseFloat(reg[key].toFixed(2)) < parseFloat(key)){
                
                console.log(reg[key] + " < " + key)
                console.log("PROBLEM NO MONEY")
                
                break;
            }

        }
        


        let a = cid[0][1] - reg[0.01];
        let b = cid[1][1] - reg[0.05];
        let c = cid[2][1] - reg[0.1];    
        let d = cid[3][1] - reg[0.25];
        let e = cid[4][1] - reg[1];
        let f = cid[5][1] - reg[5];
        let g = cid[6][1] - reg[10];
        let h = cid[7][1] - reg[20];
        let i = cid[8][1] - reg[100];


        let gimme = [
            ["PENNY", a.toPrecision(1)],
            ["NICKEL", b.toFixed(2)],
            ["DIME", c.toPrecision(1)],
            ["QUARTER", d.toFixed(1)],
            ["ONE", e.toFixed(0)],
            ["FIVE", f.toFixed(0)],
            ["TEN", g.toFixed(0)],
            ["TWENTY", h.toFixed(0)],
            ["ONE HUNDRED", i.toFixed(0)]
        ];

        
        if (reg[key] <= 0) {
            changeDue.innerHTML = "Status: CLOSED";
        } else {
            changeDue.innerHTML = "Status: OPEN";
        }

        if (change > 0) {
            changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS";
        } else {

            if (i > 0) {
                changeDue.innerHTML += "<br>" + (gimme[8][0]) + ": " + "$" + (gimme[8][1]);
            }
    
            if (h > 0) {
                changeDue.innerHTML += "<br>" + (gimme[7][0]) + ": " + "$" + (gimme[7][1]);
            }
            
            if (g > 0) {
                changeDue.innerHTML += "<br>" + (gimme[6][0]) + ": " + "$" + (gimme[6][1]);
            }
    
            if (f > 0) {
                changeDue.innerHTML += "<br>" + (gimme[5][0]) + ": " + "$" + (gimme[5][1]);
            }
    
            if (e > 0) {
                changeDue.innerHTML += "<br>" + (gimme[4][0]) + ": " + "$" + (gimme[4][1]);
            }
    
            if (d > 0) {
                changeDue.innerHTML += "<br>" + (gimme[3][0]) + ": " + "$" + (gimme[3][1]);
            }
    
            if (c > 0) {
                changeDue.innerHTML += "<br>" + (gimme[2][0]) + ": " + "$" + (gimme[2][1]);
            }
    
            if (b > 0) {
                changeDue.innerHTML += "<br>" + (gimme[1][0]) + ": " + "$" + (gimme[1][1]);
            }
    
            if (a > 0){
                changeDue.innerHTML += "<br>" + (gimme[0][0]) + ": " + "$" + (gimme[0][1]);
            }

            
        }

        
    

    });


}

There’s few global variables which get calculated only on the initial execution of the code. When cid or price is changed manually in code, the preview is reloaded, effectively executing everything again. On the other hand there’s no such reload between each test, so part of the variables are stuck with the initial values.

Sorry, I still dont really understand. Im still a noob.

Could you write what exactly you don’t understand, or how do you understand it currently? It will be easier then to help you with details.

1 Like

So I have a few global variables that get calculated on the function.
Is that causing the issue?

CID and Price get changed in the code then the preview gets reloaded.

Also no reload on the test I just cant figure out how to fix it

I’m just confused in general, not sure why what to change

On which global variables your functions depend on?

:frowning_with_open_mouth: :anguished:
bruh, I can’t. I’m going to watch a tutorial on how someone else does it.

last question: what champ do you main on league

Taking press function as example, there’s few variables declared in it, for example - cashValue, change.

function press() {
    let cashValue = parseFloat(cash.value);
    let change = parseFloat(cashValue - price);

    if (cashValue < price || cash.value.trim() === "") {
        alert("Customer does not have enough money to purchase the item");
    } else if (cashValue === price) {
        changeDue.innerHTML = ("No change due - customer paid with exact cash");
    } else if (totalStock < change) {
        console.log("Total Stock: " + totalStock)
        console.log("Price: " + price)
        changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS";
    } else {
        calc();
        console.log("total stock: " + totalStock)
        console.log("change: " + change)
    }
}

There are also such, that are not defined within function. They are from outside of the function scope, they are from global scope. Are you able to list them?

Yeah I declare those in the press function,
The ones I declare outside of the function scope are:

let price = 3.26;
let cash = document.getElementById("cash");
let changeDue = document.getElementById("change-due");
let cashValue = parseFloat(cash.value);


let cid = [
  ["PENNY", 0.1],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 1],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0]
];

let totalStock = (cid[0][1] + cid[1][1] + cid[2][1] + cid[3][1] + cid[4][1] + cid[5][1] + cid[6][1] + cid[7][1] + cid[8][1]);

let reg = {
    0.01: cid[0][1],
    0.05: cid[1][1],
    0.1: cid[2][1],
    0.25: cid[3][1],
    1: cid[4][1],
    5: cid[5][1],
    10: cid[6][1],
    20: cid[7][1],
    100: cid[8][1],        
}

I guess the cashValue doesnt need to be defined there

Yeah, pretty much that’s them.

Once the cash register is loaded, these will not change, unless changed explicitly. When running tests cid, price and #cash element are changed to match the test case requirements.

The others, in here - totalStock and reg will not change between tests, whatever value they have at the end of first test, that’s what they will have at the start of second test. This isn’t like with function when calling it again will execute code once more.

Does this make sense?

1 Like

Copying someone else’s solution from a video would violate the academic honesty policy. Please don’t do that!

Oh no just to get inspiration on how they might have done it.
I would have written it out myself again.

ohh right, I understand so I need to be defining these in the function instead
for totalStock and reg

Oh my god that worked perfectly!!
Thank you for looking at my code!!!

I moved let reg to the calc function and moved total stock to the press function and it passes

That still breaks the academic honesty policy. You cannot go looking at other people’s solutions before you have passing code

Oh ok, well I’ve got it working now