"JavaScript Algorithms and Data Structures "Projects: Cash Register

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

what’s the whole error?

1 Like

Hello Ieahleen!

Yes, is saying about the " Syntax error". But from the start I could run it, but now doesn’t let me.

Could be from “checkCashRegister” the main subject . But I am not so sure, to be honest with you.

there should not be written only “SyntaxError”, what else there is written?

1 Like

Whenever I get a syntax error, I check for missing curly braces. You are missing a curly brace at the end. The code still is not correct, but at least the tests will run now.

1 Like

Is saying on line 69 that you can see this " ] ); ". I tried to fix it several times, but I cannot find the mistake.

Could you please tell me which line is it? I was trying to do this exercise for the whole day :sweat_smile:. And again is not running the test.

Hey! I finally found the missing curl braces, many thanks! Is running now. But my exercise is still not good. Could you please help with that? I don’t know where is the mistake?

Hey! I got it where was the mistake about the “Syntax Error” Many thanks. I have another question regarding to the exercise. Only the first topic is solved. And doesn’t matter what I do I cannot not solve all of them.

I completed this project not so long ago myself. It took me days, so I’m not the best one to ask for help with this one.

But I do see that line 56, your last return statement, is greyed out. I don’t know why, I haven’t looked at all your code, but that statement isn’t running, or something is wrong with it.

Best of luck.

1 Like

Many thanks to point it out! I will try to fix it now. :wink: