Javascript cash register

i tried javascript cash register, but the test don’t pass,
but i try in onecompiler com the output is right?
here is the code

const change=[];
var status;
function checkCashRegister(price, cash, cid) {
  let c;
  c=cash-price;
  if(c/100>=1){
   c=bt(c,"ONE HUNDRED",100,cid[8][1]);
  }
  if(c/20>=1){
     c=bt(c,"TWENTY",20,cid[7][1]);
  }
  if(c/10>=1){
  c=bt(c,"TEN",10,cid[6][1]);
  }
  if(c/5>=1){
  c=bt(c,"FIVE",5,cid[5][1]);
  }
  if(c/1>=1){
 c=bt(c,"ONE",1,cid[4][1]);
  }
  if(c/0.25>=1){
 c=bt(c,"QUARTER",0.25,cid[3][1]);
  }
  if(c/0.1>=1){
   c=bt(c,"DIME",0.1,cid[2][1]);
  }
  if(c/0.05>=1){
  c=bt(c,"NICKEL",0.05,cid[1][1]);
  }
  if(c/0.01>=1){
 c=bt(c,"PENNY",0.01,cid[0][1]);
 var ct=c;
  }
  
  if(c>0){status="INSUFFICIENT_FUNDS";}
 let obj= {status: status, change: change}
  return obj;
}

function bt(c,u,a,scid){
  
   let n=Math.floor(c/a);
    let f=scid-n*a;
    if(f>0){
    change.push([u,n*a]);  
    status="OPEN";
    c=(c*100-n*a*100)/100;
    c=parseFloat(c).toFixed(2);
    }else if(scid>0){
      change.push([u,scid]);
      status="CLOSED";
      c=(c*100-scid*100)/100;
      c=parseFloat(c).toFixed(2);
    }else{
      
    }
    return c;
}

result in onecompiler com

Which test isn’t passing, can you post the test output, please?

all fail except first, but this is the one i test:
Screenshot from 2023-09-16 18-28-39

What output do you get when you test this case? (on the fCC site)

in fcc? there is no output, just test result:

log your function call to the console, so you can see your output console.log()

1 Like

like this?

This is strange. Didn’t solve it but:

{ status: ‘INSUFFICIENT_FUNDS’, change: [ [ ‘PENNY’, 0.01 ] ] }

The insufficient funds case returns a penny instead of an empty array.

c=parseFloat(c).toFixed(2);

These lines return a string. Not sure if that might be causing the problem, but it seems like it might be a type or precision problem somewhere, even though it mostly (except for the insufficient funds example) looks like it returns correct results. When you are pushing numbers to the change array they are numbers.

I’ll let you know if I find anything else.

One other thing I found, first I tried this in VSCode and I got this complexity message:

Screenshot 2023-09-16 081541

Which made me think that maybe something in the fCC is timing out and returning an error before the code returns the result, but that’s just a wild guess. I never use VSCode for JS so I’ve never seen this before.

Might not be relevant, not sure

i use toFixed because even tough i multiply by 100 before doing arithmetic and divide the result by 100, there is still one that result in a lot of decimal.

what is complexity? never use vscode, i only use sublimetext

i’ll try again tommorrow, thanks a lot.

I had a similar approach but I put everything *100 including the cash values (.05, etc.) and totally avoided any floating point math.

It is because of the top-level variables, you can’t have a call to the function inside the code. Without the function call in the code it should work (i.e. not calling it before the test does).

If you move the top-level variable and the bt function inside the main function you can have the call and it will still pass (that specific test).


I would suggest you name your variables something that actually expresses what value they contain. Your goal with code is not just to have it run it is for it to be readable and understandable by everyone, including you 4 months from now.

1 Like

Try it again?

I tested the exact same code again and now most tests are passing (except insufficient funds and CLOSED)

This is using the exact same code copied from your post, no change. Previously I was getting the same result as you, only the 1st test passed.

Now try calling the function as well.

My last test was with the unmodified code.

I had refactored it a little bit and wanted to see how it performed with the tests and it passed. I started to roll back some changes to see what made the difference and ended up copying the original code back in and it passed tests it had previously failed.

That makes me think it might have been a complexity/timeout issue? Maybe there was more resources available now? Not sure how likely this is.

Would that be related to the function call you’re talking about?

It is just the top-level array. The test doesn’t know about it and can’t reset it, so running the function and then running the test is the same as running the function twice in a row. Which also returns the wrong thing for the second run.

console.log(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]]))
// { status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }
console.log(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]]))
// { status: 'OPEN', change: [ [ 'QUARTER', 0.5 ], [ 'QUARTER', 0.5 ] ] }

If you reset the array after the function call it works as well.

console.log(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]]))
// { status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }

change.length = 0;

console.log(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]]))
// { status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }

But that is a horrible idea. The array should just be declared inside the function so it gets redeclared (reset) between function calls.

i think you are right, so i put bt function , and variable inside main function and it works, thanks a lot.

function checkCashRegister(price, cash, cid) {
  let c;
  let change=[];
  let stat;
  c=cash-price;
  function bt(c,u,a,scid){
  
   let n=Math.floor(c/a);
    let f=scid-n*a;
    if(f>0){
    change.push([u,n*a]);  
    stat="OPEN";
    c=(c*100-n*a*100)/100;
    c=parseFloat(c).toFixed(2);
    }else if(scid>0){
      change.push([u,scid]);
      stat="CLOSED";
      c=(c*100-scid*100)/100;
      c=parseFloat(c).toFixed(2);
    }else{
      
    }
    return c;
}
  if(c/100>=1){
   c=bt(c,"ONE HUNDRED",100,cid[8][1]);
  }
  if(c/20>=1){
     c=bt(c,"TWENTY",20,cid[7][1]);
  }
  if(c/10>=1){
  c=bt(c,"TEN",10,cid[6][1]);
  }
  if(c/5>=1){
  c=bt(c,"FIVE",5,cid[5][1]);
  }
  if(c/1>=1){
 c=bt(c,"ONE",1,cid[4][1]);
  }
  if(c/0.25>=1){
 c=bt(c,"QUARTER",0.25,cid[3][1]);
  }
  if(c/0.1>=1){
   c=bt(c,"DIME",0.1,cid[2][1]);
  }
  if(c/0.05>=1){
  c=bt(c,"NICKEL",0.05,cid[1][1]);
  }
  if(c/0.01>=1){
 c=bt(c,"PENNY",0.01,cid[0][1]);
  }
  
  if(c>0){
    stat="INSUFFICIENT_FUNDS";
    change=[];
  }else if(stat=="CLOSED"){
    change=cid;
  } 
 let obj= {status: stat, change: change}
  return obj;
}

as @lasjorg said it is the change array , in fcc there is code that call the function once below the function, i remove that, and it works.

It’s weird it makes no difference for me if I add or remove the last function call like that.

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