Help is needed please

Tell us what’s happening:
can please someone tell me what is wrong with my code
Your code so far


const moneyDict = {
 'PENNY': 1,
 'NICKEL': 5,
 'DIME': 10,
 'QUARTER': 25,
 'ONE': 100,
 'FIVE': 500,
 'TEN': 1000,
 'TWENTY': 2000,
 'ONE HUNDRED': 10000
};


function checkCashRegister(price, cash, cid) {
   for (let element of cid) {
       totalChange += CoinValue[1];
     }
     totalChange = totalChange.toFixed(2);  
     let cashToReturn = cash - price;
     const changeArray = [];
  
denominations.forEach(moneyType => {
   [amtLeftToPay, cidObj, changeToGive] =
     takeOutMoneyFromCid(amtLeftToPay, cidObj, changeToGive, moneyType);
 });

 if (amtLeftToPay > 0) {
   return {status: 'INSUFFICIENT_FUNDS', change: []};
 }else {
    if (cashToReturn > 0) {
   return { status: "INSUFFICIENT_FUNDS", change: [] };
  return { status: "OPEN", change: changeArray};
    }
   }
};

   **Your browser information:**

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

Challenge: Cash Register

Link to the challenge:

modifed version

const moneyDict = {

  'PENNY': 1,

  'NICKEL': 5,

  'DIME': 10,

  'QUARTER': 25,

  'ONE': 100,

  'FIVE': 500,

  'TEN': 1000,

  'TWENTY': 2000,

  'ONE HUNDRED': 10000

};

function checkCashRegister(price, cash, cid) {

    for (let element of cid) {

        totalChange += CoinValue[1];

      }

      totalChange = totalChange.toFixed(2);  

      let changeToGive = cash - price;

      const changeArray = [];

   

 denominations.forEach(moneyType => {

    [amtLeftToPay, cidObj, changeToGive] =

      takeOutMoneyFromCid(amtLeftToPay, cidObj, changeToGive, moneyType);

  });

  if (amtLeftToPay > 0) {

    return {status: 'INSUFFICIENT_FUNDS', change: []};

     } else if (changeToGive.toFixed(2) === totalCID) {

      return { status: "CLOSED", change: cid };

    }else {

     if (changeToGive > 0) {

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

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

     }

    }

}

What errors are you seeing? How is this code failing to meet the tests? This information will help others locate the error, and being able to describe issues with code is an important skill to develop.

1 Like

Roger that! I will try it again and let you know

My problem is when I run the test, I do not pass the any of the requirement.

A couple of things stand out to me.

  1. I would format your code a little bit more because it will make it easier to read these if/else chains.
  2. I am not sure why you have these two return like this here
  1. I am getting an error message that totalChange is not defined. I don’t see that in your code.

My main advice would be go to through and add in some console.logs to check if everything is doing what it is supposed.
I would manually test your code with one the test cases and make sure it is returning the results it is supposed to.

Hope that helps!

1 Like

alright! I will do it again

Can somebody tells me what is wrong?


const DOLLARDICTIONARY = {
  '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 change= Math.round(cash-price)*100;
let cashBalance={};
let amtToGive={};

cid.forEach(function(denomination){
  cashBalance[denomination[0]]=Math.round(denomination[1]*100)
})

let index= DOLLARDICTIONARY.length -1;

while (index>=0 && change>0){
  let nameOfMoney =DOLLARDICTIONARY[index][0];
  let valueOfMoney= DOLLARDICTIONARY[index][1];

  if (change-valueOfMoney >0 && cashBalance[nameOfMoney], amtToGive){
    
    amtToGive[nameOfMoney]=0;
    
while (cashBalance[nameOfMoney]>0 && change-valueOfMoney>= 0){
cashBalance[nameOfMoney] -= valueOfMoney;
amtToReturn[nameOfMoney] += valueOfMoney;
change-=valueOfMoney;
}

  }
  index +=1;
}
if (change===0){
let isRegisterEmpty=true;

Object.keys(cashaBlance).forEach(function(moneyType){
  if(cashBalance[moneyType]>0){
  }
});

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



let res = checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
console.log(res)

Please do not create duplicate topics for the same challenge/project question(s). I have combined your posts.

Thank you.

1 Like

I am new to this world. Thanks for your comprehension.
t

Meanwhile can you help?

In my response here I have formatted your code so it is easier to understand and read. I know the FCC editor doesn’t have formatting but it will make things a lot easier to find bugs if you format your code properly.

const DOLLARDICTIONARY = {
  '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 change = Math.round(cash - price) * 100;
  let cashBalance = {};
  let amtToGive = {};

  cid.forEach(function (denomination) {
    cashBalance[denomination[0]] = Math.round(denomination[1] * 100);
  });

  let index = DOLLARDICTIONARY.length - 1;

  while (index >= 0 && change > 0) {
    let nameOfMoney = DOLLARDICTIONARY[index][0];
    let valueOfMoney = DOLLARDICTIONARY[index][1];

    if ((change - valueOfMoney > 0 && cashBalance[nameOfMoney], amtToGive)) {
      amtToGive[nameOfMoney] = 0;

      while (cashBalance[nameOfMoney] > 0 && change - valueOfMoney >= 0) {
        cashBalance[nameOfMoney] -= valueOfMoney;
        amtToReturn[nameOfMoney] += valueOfMoney;
        change -= valueOfMoney;
      }
    }
    index += 1;
  }
  if (change === 0) {
    let isRegisterEmpty = true;

    Object.keys(cashaBlance).forEach(function (moneyType) {
      if (cashBalance[moneyType] > 0) {
      }
    });

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

A couple of things I noticed.

  1. if you console.log index it returns NaN
 let index = DOLLARDICTIONARY.length - 1;
 console.log(index) // returns NaN

So that will affect your while loop.

But I am also confused here

You are treating DollarDictionary like a 2d array but it is not.

If you console.log amtToGive before the end of your function then you will just get an empty object.

As I mentioned earlier, if you add console.logs as your building it then you can catch some of those errors that I mentioned. Like the issue with the index returning NaN.

Hope that helps!

what should I use as a second array?

I am not sure I understand what you are asking.

Use a second array where?
It is hard to tell without context.

Can you post your updated code?

hello, I am referring to this.
So that will affect your while loop.

But I am also confused here

You are treating DollarDictionary like a 2d array but it is not.

If you console.log amtToGive before the end of your function then you will just get an empty object.

2d array as in two dimensional array.
Not second array.

You defined dollar dictionary as an object literal.

cid is an example of a 2D array

[["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]

What was the goal of these lines here

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