Test cases don't confirm the answer's right

When I try my function with exactly the same arguments in test cases, I get the same result as I should get, however test cases don’t confirm they’re same

``````  **ALERT: Below is spoilers for cash register challenge**
``````
``````
let names = ["PENNY","NICKEL","DIME","QUARTER","ONE","FIVE","TEN","TWENTY","ONE HUNDRED"]
let values = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100]
function checkCashRegister(price, cash, cid) {
let reserves =[] // how many banknote we have in register, penny, nickel, dime order
for (let banknote of cid)
{
reserves.push(Math.round(banknote[1]/(values[names.indexOf(banknote[0])])))
}
let change = cash - price
names = names.reverse()
values = values.reverse()
reserves = reserves.reverse()
let changes = [];
//console.log("Change = ",change)
for (let value of values)
{
let count = 0
while (change >= value && reserves[values.indexOf(value)]>0)
{
reserves[values.indexOf(value)]--
change -= value
change = change.toFixed(2)
count ++
}
if (count == 0)
continue
changes.push([names[values.indexOf(value)],value*count])
}
if (change > 0)
return {status: "INSUFFICIENT_FUNDS", change: []}

let case_sit = []
for (let reserve of reserves)
{
if (reserve != 0)
{
return {status: "OPEN", change: changes}
}
}

return {status: "CLOSED", change: changes}
}

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]]))

``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36`

Challenge: Cash Register

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

``````var myGlobal = [1];
function returnGlobal(arg) {
myGlobal.push(arg);
return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
var myLocal = [1];
myLocal.push(arg);
return myLocal;
} // reliable - always returns an array of length 2
``````

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