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;
}