Cash Register Console log shows correct answer but the tests fail

I know now there would have been a much more efficient and shorter way to have solved this but it seems like it should still work. When i check the results with the console.log the correct output shows up but when i run the tests they fail and it never stops showing the tests running… in the console.log. Ive searched the forum but cant find anything similar.



unction checkCashRegister(price, cash, cid) {
  let change = cash - price 

 
 let changeArr = []
 let count = 0
let status = ""

for (let i=0; i<1000; i++){
  
   if (change < 100 || cid[8][1] < 100){
     break;
   }
   if (change >= 100) {

count = count + 100;
change = change - 100;
cid[8][1] = cid[8][1] - 100;
change = change.toFixed(2);

  }
   }

if (count > 0) {changeArr.push(["ONE HUNDRED", count.toFixed(0)])}

count = 0
for (let i=0; i<1000; i++){
  
   if (change < 20 || cid[7][1] < 20){
     break;
   }
   if (change >= 20) {

count = count + 20;
change = change - 20;
cid[7][1] = cid[7][1] - 20
change = change.toFixed(2);

  }
   }

if (count > 0) {changeArr.push(["TWENTY", count.toFixed(0)])}

count = 0
for (let i=0; i<1000; i++){
  
   if (change < 10 || cid[6][1] < 10){
     break;
   }
   if (change >= 10) {

count = count + 10;
change = change - 10;
cid[6][1] = cid[6][1] - 10
change = change.toFixed(2);

  }
   }

if (count > 0) {changeArr.push(["TEN", count.toFixed(0)])}

 
count = 0
for (let i=0; i<1000; i++){
  
   if (change < 5 || cid[5][1] < 5){
     break;
   }
   if (change >= 5) {

count = count + 5;
change = change - 5;
cid[5][1] = cid[5][1] - 5
change = change.toFixed(2);

  }
   }

if (count > 0) {changeArr.push(["FIVE", count.toFixed(0)])}

 
count = 0
for (let i=0; i<1000; i++){
  
   if (change < 1 || cid[4][1] < 1){
     break;
   }
   if (change >= 1) {

count = count + 1;
change = change - 1;
cid[4][1] = cid[4][1] - 1
change = change.toFixed(2);

  }
   }

if (count > 0) {changeArr.push(["ONE", count.toFixed(0)])}

 
count = 0
for (let i=0; i<1000; i++){
  
   if (change < 0.25 || cid[3][1] < 0.25){
     break;
   }
   if (change >= 0.25) {

count = count + 0.25;
change = change - 0.25;
cid[3][1] = cid[3][1] - 0.25;
change = change.toFixed(2);

  }
   }

if (count > 0) {changeArr.push(["QUARTER", count.toFixed(2)])}

 
count = 0
for (let i=0; i<1000; i++){
  
   if (change < 0.09 || cid[2][1] < 0.09){
     break;
   }
   if (change >= 0.1) {

count = count + 0.1;
change = change - 0.1;
cid[2][1] = cid[2][1] - 0.1
change = change.toFixed(2);

  }
   }

if (count > 0) {changeArr.push(["DIME", count.toFixed(1)])}

 
count = 0
for (let i=0; i<1000; i++){
  
   if (change < 0.049 || cid[1][1] < 0.049){
     break;
   }
   if (change >= 0.05) {

count = count + 0.05;
change = change - 0.05;
cid[1][1] = cid[1][1] - 0.05;
change = change.toFixed(2);

  }
   }

if (count > 0) {changeArr.push(["NICKEL", count.toFixed(2)])}

count = 0
for (let i=0; i<1001; i++){
  
   if (change < 0.009 || cid[0][1] < 0.009){
     break;
   }
   if (change >= 0.01) {

count = count + 0.01;
change = change - 0.01;
cid[0][1] = cid[0][1] - 0.01;
change = change.toFixed();

  }
   }

if (count > 0) {changeArr.push(["PENNY", count.toFixed(2)])}

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

  
if (change > 0) {
  status = "INSUFFICIENT_FUNDS"
}
if (count == 0){
  status = "OPEN"
}
if (count == 0 && cidTotal == 0){
  status = "CLOSED"
}

 let changeOb = {
status: status,
change: changeArr,
}
console.log(changeOb)
return changeOb

}




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]]);

What do the failing tests say?
What have you tried so far in terms of debugging those tests?

When i hit run the tests the first test passes but the rest don’t and in the console log the tests never conclude running. When i put the data into the function at the bottom the console log variable that i am returning shows the correct answer.

What do the failing tests say?

the first one shows a check and the rest are X. Maybe my code is too long and is messing up the results given?

I may try a different browser ive read somtime switching the browser helps

Just when i thought i was done

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