Tell us what’s happening:
Hi, So I’m getting a syntax error on line 60 of the Cash Register project saying I can’t use ‘return’ outside a function, but as far as I can tell it’s inside a function? Any help would really be appreciated!
Your code so far
//currency in pennies
const BILLS = [
['ONE HUNDRED', 10000],
['TWENTY', 2000],
['TEN', 1000 ],
['FIVE', 500],
['ONE', 100],
['QUARTER', 25],
['DIME', 10],
['NICKEL', 5],
['PENNY', 1],
];
function checkCashRegister(price, cash, cid) {
var requiredChange = cash * 100 - price * 100;
var yourCash = {};
var myCash = {};
var i = 0;
//if no change
if (change === 0) {
return {
status: "CLOSED",
change: cid
};
}
//switch the array for an object;
cid.forEach(money => {
myCash[money[0]] = parseInt(money[1] * 100)
});
//from big bills to small
while (i < BILLS.length && change > 0) {
var billName = BILLS[i][0];
var billValue = BILLS[i][1];
}
//Accept and give change
if (change - billValue > 0 && myCash[billname] > 0) {
yourCash[billName] = 0;
while (change - billValue >= 0 && myCash[billName] > 0) {
yourCash[billName] += billValue / 100;
myCash[billName] = parseInt(myCash[billName - billVaue]);
change -= billValue;
}
}
i++;
}
if (change === 0) {
let hasMoney = false;
Object.keys(myCash).forEach(key => {
if (myCash[key] > 0) {
hasMoney = true;
}
});
if (hasMoney) {
return {
status: 'OPEN',
change:
Object.keys(yourCash).map(key => {
let obj = [key, yourCash[key]];
console.log(JSON.stringify(obj));
return obj;
})};
} else {
console.log('NO money left...');
return {
status: 'CLOSED',
change: 'cid',
};
}
}
return {
status: 'INSUFFICIENT_FUNDS',
change: []
}
// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]
checkCashRegister(19.5, 20, [["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 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/