Project: Cash Register

Hello, I need some help with my Cash Register project solution.
I have checked the test cases locally (dev console) and they seem fine there. However, when I submit, I only pass the first two test cases, although I get the correct result on the browser’s debug console.
I am thankful for any suggestions. Here is my code:

function checkCashRegister(price, cash, cid) {
     let expected_change = cash - price
    let money = [0.01, 0.05, 0.10, 0.25, 1.00, 5.00, 10.00, 20.00, 100.00]
    let change_array = []
    let money_index = 0
    let money_object = null
    let result_object = { status: "", change: [] }

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

        if (expected_change > money[i]) {
            money_index = i;
            money_object = cid[money_index];

            if (money_object[1] >= expected_change && money_object[1] != 0) {

                expected_change = expected_change.toFixed(2)
                let temp_amount = Math.floor(expected_change / money[money_index]) * money[money_index]

                let temp_array = [money_object[0], temp_amount]
                expected_change = (expected_change - temp_amount)

                change_array.push(temp_array)


            } else if (expected_change > money_object[1]) {
                enough_change = true
                temp_array = [money_object[0], money_object[1]]
                expected_change = expected_change - money_object[1]
                change_array.push(temp_array)

            }

        } else if (expected_change < money[i]) {
            money_index = i;
            money_object = cid[money_index];
            if (money_object[1] == 0) {
                change_array.push(money_object)
            }
        }

    }

    change_array.reverse()
    if (JSON.stringify(change_array) == JSON.stringify(cid) && expected_change == 0) {
        result_object.status = "CLOSED"
        console.log(expected_change)
        result_object.change = cid
        return result_object
    }
    if (expected_change > 0) {
        result_object.status = "INSUFFICIENT_FUNDS"
        result_object.change = []
    } else {
        result_object.status = "OPEN"
        result_object.change = change_array.reverse()
    }

    return result_object;
}

I’ve edited your code for readability. 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 it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

If I run one of the failing function calls

console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]))

I see this.

ReferenceError: enough_change is not defined

If I fix that by declaring the variable, I see this.

ReferenceError: temp_array is not defined

If I look for that identifier, I can see it is declared inside an if statement but then referenced outside it. Variables declared with let are block scoped.

Make sure you declared your variables and pay attention to the scope they are in.


The challenges run in strict mode

1 Like

Thank you, I that was really helpful. I will make sure I format my code like that in any future help posts.

Thank you so much, I was not aware of the strict mode. Once I solved the reference errors, my code passes all test cases.

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