JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
I have a question and one big problem to hopefully get me started with this challenge.
I don’t know if my thinking is somehow strange but is the cid array supposed to present that I have 1,01 dollars worth of pennies or that I have 1,01 pennies?
I assumed that it would mean I have 101 pennies.

With that assumption I have been thinking of ways to figure out how to return the correct amount of money in correct change. For example the third test ask me to return 96,74 dollars but how on earth should I be able to return the correct amount.
Only way I have found out to be even nearly writeable in code is to have variables for every different type of money and also to have variables for the amounts of said types and then somehow subtract the amounts from the sum to be returned. This however can’t be the means to solve it as it would require atleast 20 different variables.

This challenge seems super complicated for the skill level I have acquired from all the previous challenges and I don’t believe I am able to get it done.
I’ve now spent two days burning through my paper notepad without any real progress on this.

Your code so far

function checkCashRegister(price, cash, cid) {
  let change;
  return 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]]);

Your browser information:

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

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

It means that if you add up the total value of all the penny coins it will be worth $1.01
(And that means that the total number of penny coins is 101)

Code written this far:

function checkCashRegister(price, cash, cid) {
  let moneyValues = [
    100,
    20,
    10,
    5,
    1,
    0.25,
    0.1,
    0.05,
    0.01
  ]

  let totalCID = 0

  for (let j = 0; j < cid.length; j++) {
    totalCID += cid[j][1]
  }

  //console.log(totalCID)

  let drawer = cid.reverse();
  let returnSum = cash - price;

  let changeMoney = [];
  //let testArray = [];


  for (let i = 0; i < cid.length; i++) {
    let moneyCount = drawer[i][1] / moneyValues[i]
    let returnMoneyCount = 0;
    while (moneyValues[i] <= returnSum && moneyCount > 0) {
      returnSum -= moneyValues[i];
      moneyCount--;
      returnMoneyCount++;
    }
    if (returnMoneyCount * moneyValues[i] > 0) {
      changeMoney.push([cid[i][0], returnMoneyCount * moneyValues[i]])
    }
  }
  console.log(cid + " cid")
  console.log(totalCID + " totalCID")
  console.log(returnSum + " returnSum")
  console.log(changeMoney + " changeMoney")

  //insufficient
  if (totalCID < cash) {
    let toReturn = { status: "INSUFFICIENT_FUNDS", change: [] }
    return toReturn
    //open
  } else if (changeMoney.length > 0) {
    let toReturn = { status: "OPEN", change: changeMoney }
    return toReturn
    //closed
  } else if (totalCID === returnSum) {
    let toReturn = { status: "CLOSED", change: cid }
    console.log(toReturn)
    return toReturn
  }
}

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

Judging by what console.log(changeMoney + " changeMoney") prints out I have a problem in rounding the values as the test case I’m using loses 0.01 dollars in the process from the cash-in-drawer.

Edit: Accidental ctrl + enter during writing
I can’t seem to find a way to correctly round my changeMoney variable. I’ve tried doing it every situation I can think of but it just wont round correctly into 0.50.

That is probably a floating point rounding error. You can deal with that by not using floating numbers and using integers. You can convert the numbers back and forth from floats to integers with some math. There are also some built in methods that can help round them as well, if you want to search through the MDN site.

Thank you for trying but to be honest I don’t understand your reply at all.
How could I use integers instead of floats as the numbers given in the test cases are not integers only?

One way is to use cents instead of dollars when doing the calculations.

Yes the test is sending floats and expects floats back, but like i said you can convert floats to integers and integers to floats. I gave a somewhat vague answer because these are the projects, i want you to do as much googling as you can and look into this yourself before i give bigger hints on how to deal with this. This problem has probably been asked about a lot on these very forums. And like a said there are some methods that can help, but i personally find the integer route easier when you figure out how to convert those numbers.

It has to be toFixed() you are referring to as every other way I have found this far returns just the integer without decimals and that wont work.
I haven’t found a working way to use toFixed() today nor for two days before this.

Seems like it is time to scrap the code again and start over.

Why wouldn’t using integers work? That’s how I solved it.

Integers aren’t floats?
If I have a number with decimals, say 1.5 and I call it integer it would be 1 or 2 depending on the used rules of rounding.
In the challenge I need to use decimals right? So how could integer work if it rounds 19.5 up to 20?

True but…

Not true


If you use dollars, then you are forced into using decimals to represent cents. But dollars are 100 cents each, so you can use an integer number of cents instead of a decimal number of dollars.

If you want to use integers to do this, you don’t want to just round the number. You need to shift that decimal point over so that .01 becomes 1 if you are using pennies.