理解javascript return语句为什么返回undefined

时间:2018-05-02 18:24:23

标签: javascript

我正在处理以下javascript代码,有人可以解释为什么下面的代码输出是undefined吗?

鉴于一年,根据该年度的俄罗斯官方日历查找该年256th天的日期。

然后以dd.mm.yyyy格式打印。

function solve(year) {
  if (year < 1700 || year > 2700) {
    return false;
  } else if (year >= 1700 && year <= 1917) {
    if (year % 4 === 0) {
      var days = [31, 29, 31, 30, 31, 30, 31, 31];
      calculate(days)
      `enter code here`
    }
    var days = [31, 28, 31, 30, 31, 30, 31, 31];
    calculate(days)
  } else if (year > 1918 && year <= 2700) {
    if (year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
      var days = [31, 29, 31, 30, 31, 30, 31, 31];
      calculate(days)
    }
    var days = [31, 28, 31, 30, 31, 30, 31, 31];
    calculate(days)
  } else if (year === 1918) {
    var days = [31, 15, 31, 30, 31, 30, 31, 31];
    calculate(days)
  }

  function calculate(days) {
    var daysOfSep = 256 - days.reduce(function(accumulator, currentValue) {
      return accumulator + currentValue
    }, 0)

    return `${daysOfSep}.09.${year}`
  }
}

console.log(solve(1918))

2 个答案:

答案 0 :(得分:2)

因为您没有在main函数中返回,所以默认情况下函数表达式返回undefined:

function solve(year){
        if(year < 1700 || year > 2700) {
          return false;
        }else if(year >= 1700 && year <= 1917) {
          if(year%4 === 0) {
            var days = [31,29,31,30,31,30,31,31];
            calculate(days)`enter code here`
      }
          var days = [31,28,31,30,31,30,31,31];
          calculate(days)
       }else if(year > 1918 && year <= 2700) {
         if(year % 400 === 0 || (year%4 === 0 && year%100 !== 0)) {
         var days = [31,29,31,30,31,30,31,31];
         calculate(days)
       }
      var days = [31,28,31,30,31,30,31,31];
      return calculate(days) //calculate inner function returns the result, but you do not return this.
    }else if(year === 1918) {
      var days = [31,15,31,30,31,30,31,31];
      return calculate(days) //calculate inner function returns the result, but you do not return this.
    }

        function calculate (days) {
          var daysOfSep = 256 - days.reduce(function(accumulator, currentValue) {
           return accumulator + currentValue
          },0)

          return `${daysOfSep}.09.${year}`
        }
    }

    console.log(solve(1918))
 26.09.1918

答案 1 :(得分:1)

当您首先调用solve()函数并再次调用solve()函数内部时,您正在调用calculate()函数。您的calculate()函数内部的返回值不在solve()函数中,这是您获得undefined的原因

您的代码应该是这样的。

DEMO

function solve(year) {
  if (year < 1700 || year > 2700) {
    return false;
  } else if (year >= 1700 && year <= 1917) {
    if (year % 4 === 0) {
      var days = [31, 29, 31, 30, 31, 30, 31, 31];
      calculate(days)
      `enter code here`
    }
    var days = [31, 28, 31, 30, 31, 30, 31, 31];
    return calculate(days)
  } else if (year > 1918 && year <= 2700) {
    if (year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
      var days = [31, 29, 31, 30, 31, 30, 31, 31];
     return  calculate(days)
    }
    var days = [31, 28, 31, 30, 31, 30, 31, 31];
    calculate(days)
  } else if (year === 1918) {
    var days = [31, 15, 31, 30, 31, 30, 31, 31];
   return  calculate(days)
  }

  function calculate(days) {
    var daysOfSep = 256 - days.reduce(function(accumulator, currentValue) {
      return accumulator + currentValue
    }, 0)

    return `${daysOfSep}.09.${year}`
  }
}

console.log(solve(1918))