Cash Register -help

function checkCashRegister(price, cash, cid) {

let change = cash-price;

let unit = [['ONE HUNDRED', 0], ['TWENTY', 0] , ['TEN', 0], ['FIVE', 0], ['DOLLAR', 0], ['QUARTER', 0], ['DIME', 0], ['NICKEL', 0], ['PENNY', 0]]

let value = [100, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01]

let cidValues = []

let numbers = []

let sum = 0

let open = []

let closed = []

for (var i = 0; i < cid.length; i++){

  sum = sum + cid[i][1]

  cidValues.unshift(cid[i][1]) // listing the amount of each unit

} // finding sum of cash in drawer

for (var k = 0; k < value.length; k++){

  numbers.push(Math.round(cidValues[k]/value[k]))

}

console.log(value)

for(let j = 0; j < value.length; j++){ // LOOPING THRU TO GET TOTALS

  let total = 0

  while(change > 0 && change >= value[j] && cidValues[j] > 0){

    Math.ceil(change -= value[j])

    Math.ceil(total += value[j])

    Math.ceil(cidValues[j] -= value[j])

  } 

  unit[j][1] = total

  console.log(unit)

  console.log(change)

} 

}

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

My results are off by just 0.01 cents for the penny array. Does anyone know how I can round up decimals without just turning them into integers?

hey, take a look at my answers in this other thread, see if they can help you, it seems you have the same issue (yep, only way):

2 Likes

I recommend using an integer number of cents for everything. I know this is not what you want to hear, but with floating point values roundoff will always happen.

I moved your question to its own topic because you were asking a question related to your own code for a challenge and were not answering the OP of the other thread. It is always best to create your own thread for you specific question(s). Also, it is advisable to use the Ask for Help button on the challenge, so it auto-populates with your current code and the challenge url.

Thank you.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

Please use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.

markdown_Forums