Having some issues with final project

Tell us what’s happening:
Between my codes, i am keep getting wrong value for my “dif variable”. Codes should make it integer(at leats two digits after the comma) but they are making decimal. That creates error. How can i make it ?

Thanks for all attention.

  **Your code so far**

function checkCashRegister(price, cash, cid) {
let dif = cash - price;
let arr = [];
let result = {};
let total = 0;
for(let i = 0; i < cid.length; i++){
  total += cid[i][1];
}
let check = price + total === cash;


if(dif >= 100){
 if(dif >= cid[8][1]){dif -= cid[8][1]; arr.unshift(cid[8]);}
 else{cid[8][1] -= dif - (dif % 100); dif = dif % 100; arr.unshift(cid[8]);}}
 else{cid[8][1] = 0; arr.unshift(cid[8]);}
 
if(dif >= 20){
 if(dif >= cid[7][1]){dif -= cid[7][1]; arr.unshift(cid[7]);}
 else{cid[7][1] -= dif - (dif % 100); dif = dif % 100; arr.unshift(cid[7]);}}
 else{cid[7][1] = 0; arr.unshift(cid[7]);}

if(dif >= 10){
 if(dif >= cid[6][1]){dif -= cid[6][1]; arr.unshift(cid[6]);}
 else{cid[6][1] = Math.floor(dif/10) * 10; dif -= cid[6][1]; arr.unshift(cid[6]);}}
 else{cid[6][1] = 0; arr.unshift(cid[6]);}


if(dif >= 5){
 if(dif >= cid[5][1]){dif -= cid[5][1]; arr.unshift(cid[5]);}
 else{cid[5][1] = Math.floor(dif/5) * 5; dif -= cid[5][1]; arr.unshift(cid[5]);}}
 else{cid[5][1] = 0; arr.unshift(cid[5]);}


if(dif >= 1){
 if(dif >= cid[4][1]){dif -= cid[4][1]; arr.unshift(cid[4]);}
 else{cid[4][1] = Math.floor(dif/1) * 1; dif -= cid[4][1]; arr.unshift(cid[4]);}}
 else{cid[4][1] = 0; arr.unshift(cid[4]);}

if(dif >= 0.25){
 if(dif >= cid[3][1]){dif -= cid[3][1]; arr.unshift(cid[3]);}
 else{cid[3][1] = Math.floor(dif/0.25) * 0.25; dif -= cid[3][1]; arr.unshift(cid[3]);}}
 else{cid[3][1] = 0; arr.unshift(cid[3]);}

if(dif >= 0.1){
 if(dif >= cid[2][1]){dif -= cid[2][1]; arr.unshift(cid[2]);}
 else{cid[2][1] = Math.floor(dif/0.1) * 0.1; dif -= cid[2][1]; arr.unshift(cid[2]);}}
 else{cid[2][1] = 0; arr.unshift(cid[2]);}

if(dif >= 0.05){
 if(dif >= cid[1][1]){dif -= cid[1][1]; arr.unshift(cid[1])}
 else{cid[1][1] = Math.floor(dif/0.05) * 0.05; dif -= cid[1][1]; arr.unshift(cid[1]);}}
 else{cid[1][1] = 0; arr.unshift(cid[1]);}

if(dif >= 0.01){
 if(dif >= cid[0][1]){dif -= cid[0][1]; arr.unshift(cid[0])}
 else{cid[0][1] = Math.floor(dif/0.01) * 0.01; dif -= cid[0][1]; arr.unshift(cid[0]);}}
 else{cid[0][1] = 0; arr.unshift(cid[0]);}

 let match = [];
 for(let i = 0; i < arr.length; i++){
   if(arr[i][1] > 0){
     match.push(arr[i]);
   }
 }
 
if(dif > 0){
  result.status = "INSUFFICIENT_FUNDS";
  result.change = [];
}
else if(check){
  result.status = "CLOSED";
  result.change = arr;
}
else{result.status = "OPEN";
     result.change = match;}
console.log(result);
return result;
}

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]]);
  **Your browser information:**

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

Challenge: Cash Register

Link to the challenge:

When I properly format your code to make it readable, it is over 150 lines.

  1. Do you expect us to read through 150 lines to figure out what you mean and where?
  2. I know this is a difficult challenge, but I think that that is way too many lines. You might want to rethink your approach.

But again, this is a hard challenge. I remember struggling with this one.

1 Like

This video may be helpful to you:

1 Like

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