JavaScript Algorithms and Data Structures Projects - Cash Register Syntax Error?

So its giving me a syntax error on line 45 for return outside of function. I do nit understand why its reading syntax error. Please help.

const DENOMINATIONS = [
  ["PENNY", 1], ["NICKLE", 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(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 - moneyValue >= 0) {
    cashOnHand[moneyName] -= moneyValue;
    cashToGive[moneyName] += moneyValue;
    amountToReturn -= moneyValue;
    }
  }
index -= 1;
}

if (amountToReturn === 0) {
  let isRegisterEmpty = true;

  Object.keys(cashOnHand).forEach(moneyType => {
    if (cashOnHand[moneyType] > 0) {
      isRegisterEmpty = true;
    }
  });

  if (isRegisterEmpty) {
    return { status: "CLOSED", 
    change: cid 
    }
  }else {
    let changeArray = [];
    Object.keys(cashToGive).map(moneyType => {
      if (cashToGive[moneyType] > 0) {
        changeArray.push([moneyType, cashToGive[moneyType] / 100]);
      };
    });

    return { status: "OPEN", change: changeArray};
  }

}
return { status: "INSUFFICIENT_FUNDS", change: []}
}

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

console.log(res);

Your browser information:

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

Challenge Information:

JavaScript Algorithms and Data Structures Projects - Cash Register

Is line 7 to 57 meant to be in the same funktion?

1 Like

No its not, but thank U Ill take a look at that.

1 Like

You closed the function on line 33

Looking at the while loop on line 18 I assume you meant to close that loop on line 33 and not where it is closed now because the variables moneyName and moneyValue are scoped to the while loop body but are used outside it.