Custom function created via app script (google sheets) is giving a blank cell. No error is shown

I typed text given in the bottom of this post, in app script. I am getting value for the =Deckel(200,1320) function as 620. For the second function =Dn(200,1320), I am getting a blank cell. What do I do to display value of n in the cell? Thank you.

function Deckel(x,y) {
let d;
for (let n=0; x*n+20 < 580;n++){d=x*(n+1)+20;}
if (d>y) {return "NOT VALID";} else {return d;}  
}

function Dn(x,y) {
let d;
let n;
for (let n=0; x*n+20 < 580;n++){d=x*(n+1)+20;}
if (d>y) {return "NOT VALID";} else {return n;}  
} 

>Solution :

In your showing script, how about the following modification?

Modified script:

function Dn(x, y) {
  let d;
  let n;
  for (n = 0; x * n + 20 < 580; n++) {
    d = x * (n + 1) + 20;
  }
  if (d > y) {
    return "NOT VALID";
  }
  return n;
}
  • In your showing script, n is used in the loop by declaring let n=0. So, I removed let of let n=0. By this, n is returned.

Reference:

Leave a Reply