Tell us what’s happening:
Hello Guys!
I’ve got stuck in this exercise. Does not let me even run it anymore. Is saying about “Syntax Error”. I have got confused about where could possibly be the error.
Thanks in advance!
C.B.
Your code so far
const DENOMINATIONS = [
["PENNY", 1], ["NICKEL", 5], ["DIME", 10], ["QUARTER", 25], ["ONE", 100], ["FIVE", 500], ["TEN", 1000], ["TWENTY", 2000], ["ONE HUNDRED", 10000]
];
function checkCashRegister(price, cash, cid) {
let amountToReturn = Math.round(cash * 100) - Math.round(price * 100);
let cashOnHand = {};
let cashToGive = {};
cid.forEach(function(denomination) {
cashOnHand[denomination[0]] = Math.round(denomination[1] * 100)
})
let index = DENOMINATIONS.length - 1;
while (index >= 0 && amountToReturn > 0) {
let moneyName = DENOMINATIONS[index][0];
let moneyValue = DENOMINATIONS[index][1];
if (amountToReturn - moneyValue > 0 && cashOnHand[moneyName], amountToReturn) {
cashToGive[moneyName] = 0;
while (cashOnHand[moneyName] > 0 && amountToReturn - moneyName > 0) {
cashOnHand[moneyName] -= moneyValue;
cashToGive[moneyName] += moneyvalue;
amountToReturn -= moneyValue;
}
}
index -= 1;
}
if (amountToReturn === 0) {
let isRegisterEmpty = true;
Object.keys(cashOnHand).forEach(function(moneyType) {
if (cashOnHand[moneyType] > 0) {
isRegisterEmpty = false;
}
});
if (isRegisterEmpty) {
return {
status: "CLOSED",
change: cid
}
} else {
let changeArray = [];
Object.keys(cashToGive).map(function(moneyType) {
if (cashToGive[moneyType] > 0) {
changeArray.push([moneyType, cashToGive[moneyType] / 100]);
};
});
return {status: "OPEN", change: changeArray};
}
return {status: "INSUFICIENT_FUNDS", change: []}
}
checkCashRegister(19.5, 20, [
["PENNY", 1.01],
["NICKEL", 2.05],
["DIME", 3.1],
["QUARTER", 4.25],
["ONE", 90.0],
["FIVE", 55.0],
["TEN", 20.0],
["TWENTY", 60.0],
["ONE HUNDRED", 100.0]
]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
.
Challenge: Cash Register
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-registerPreformatted text