为什么我在此函数中未定义?

时间:2016-08-07 18:03:07

标签: javascript

我有一个问题,保姆根据时间收取不同的费率:

保姆 - 不早于下午5:00开始 - 不迟于凌晨4:00离开 - 从开始时间到睡觉时间每小时12美元 - 从睡前到午夜每小时8美元 - 从午夜到工作结束,每小时收费16美元 - 获得整整小时的工资(无小时)

我想知道为什么我在这个函数中经过时间时会得到未定义的东西?

 function calculatePay (startTime, bedTime, endTime){

      function formatTime(time){
        if (time.indexOf('00') === -1){
          time = Number(time.split(":").shift()) + 1;
        } else {
          time = Number(time.split(":").shift());
        }
        if (time < 5) {
          time = time + 12;
        }
        return time;
      };

      var start = formatTime(startTime);
      var bedtime = formatTime(bedTime);
      var end = formatTime(endTime);

      var scheduleRange = {
        start: 5,
        lateNight: 12,
        end: 16,
      }

      var payrate = {
        beforeBed: 12,
        sleeping: 8,
        afterMidnight: 16
      }

      var calculateBeforeBed = function (start, bedtime) {
          if (bedtime > start && bedtime <= scheduleRange.lateNight){
          var beforeBedEarned = (bedtime - start) * payrate.beforeBed;
          return beforeBedEarned;
        } else if (bedtime > scheduleRange.lateNight) {
          var beforeBedEarned = (scheduleRange.lateNight - start) * payrate.beforeBed;
          return beforeBedEarned;
        } else {
          return 0;
        }
      };

      var calculateAfterBed = function (start, bedtime, end) {
        if (bedtime > start && bedtime <= scheduleRange.lateNight && bedtime <= end) {
          var afterBedEarned = (scheduleRange.lateNight - bedtime) * payrate.sleeping;
          return afterBedEarned;
        } else if (bedtime <= start && end <= scheduleRange.lateNight){
          afterBedEarned = (end - start) * payrate.sleeping;
          return afterBedEarned;
        } else if (bedtime <= start && end > scheduleRange.lateNight){
          afterBedEarned = (scheduleRange.lateNight - start) * payrate.sleeping;
          return afterBedEarned;
        }
        else {
          return 0;
        }
      };

      var calculateAfterMidnight = function (start, end) {
        if (end > scheduleRange.lateNight && start <= scheduleRange.lateNight) {
          var lateNightEarned = (end - scheduleRange.lateNight) * payrate.afterMidnight;
          return lateNightEarned;
        } else if (end > scheduleRange.lateNight && start > scheduleRange.lateNight) {
          lateNightEarned = (end - start) * payrate.afterMidnight;
          return lateNightEarned;
        } else {
          return 0;
        }
      };

      function finalInvoice (start, bedtime, end){
        if(start >= scheduleRange.start && end <= scheduleRange.end){
          var pay = calculateBeforeBed(start, bedtime) + calculateAfterBed(start, bedtime, end)+ calculateAfterMidnight(start, end);
          return pay;
          };
        };

    }

    calculatePay("7:00", "10:00", "1:00");

1 个答案:

答案 0 :(得分:1)

因为您的功能没有return声明

function voidsum(a, b, c) {
  var result = a + b + c;
}

function sum(a, b, c) {
  var result = a + b + c;
  
  return result;
}

console.log('voidfn', voidsum(123, 321, 231));
console.log('sum', sum(123, 321, 231));