简体   繁体   中英

Why return function with arguments is undefined?

I tried for many hours but I don't understand why return is undefined...

This is my code:

var x = 30.858;
var id = 584;

var num_reg = region(x);
if (num_reg == false) {
  num_reg = ripeti_region(x);
}


function ripeti_region(lng) {
  if (lng <= 180) {
    lng = lng - 0.2;
  } else if (lng => -180) {
    lng = lng + 0.2;
  }

  if (region(lng) != false) {
    var x = region(lng);   
    return x;
  } else {
    ripeti_region(lng);
  }
}


function region(lng) {
  if (lng <= 30.458) {
   return id;
  } else {
   return false;
  }
}


alert(num_reg);

If I alert num_reg I have undefined return, but the code works in fact in function ripeti_region(lng) if you add alert(x) it prints var id

So I would that num_reg give like output var id value.

This is jsfiddle: https://jsfiddle.net/nj4duw61/

Your problem is here:

else {
    ripeti_region(lng);
  }

This part of code doesn't return anything.

Probably you wanted to return ripeti_region(lng);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM