虽然返回了结果,但控制台日志未定义

时间:2017-07-11 17:44:31

标签: javascript algorithm

我在其中一个网站上解算算法,但我实际上无法测试我的代码,因为控制台显示未定义,但返回结果。请问有人可以告诉我代码有什么问题吗?

代码

function formatDuration (seconds) {
  // Complete this function
  var minutes = Math.round(seconds / 60);
  var thisSeconds = Math.round(seconds % 60);
  var hours = Math.round(seconds / 3600);
  var days = Math.round(seconds / 86400);
  var years = Math.round(seconds / 31536000);
  var stringYear = "year";
  var stringDay = "day";
  var stringHour = "hour";
  var stringMinute = "minute";
  var stringSecond = "second";

  if (years > 1) {
    stringYear = "years";  
  } else if (days > 1) {
    stringDay = "days";
  } else if (hours > 1) {
    stringHour = "hours";
  } else if (thisSeconds > 1) {
    stringSecond = "seconds";  
  } else if (minutes > 1) {
    stringMinute = "minutes"; 
  }

  if (minutes >= 60) {
    hours = Math.round(minutes/60);
    minutes = Math.abs(minutes - hours*60);
    if (minutes > 1) {
      stringMinute = "minutes"; 
    }
  } 

  if (hours >= 24) {
    hours = hours - 24;  
  }

  if (days > 365) {
    days = Math.abs(days - years*365);
  }

  var obj = new Map();
  obj.set(stringYear, years);
  obj.set(stringDay, days);
  obj.set(stringHour, hours);
  obj.set(stringMinute, minutes);
  obj.set(stringSecond, thisSeconds);

  var empArr = [];

  obj.forEach(function (value, key, mapObj) {

    if (value == 0) {
      obj.delete(key);   
    }

    var res = value + " " + key;

    if (value > 0) {
      empArr.push(res);
      formatStrings(empArr);
    }


  }); 

}

function formatStrings(arr) {
    return arr.length == 1 ? arr[0] : arr.slice(0, arr.length - 1).join(", ") + " and " + arr[arr.length - 1];
    //console.log(str);
}


formatDuration(3600);

enter image description here

1 个答案:

答案 0 :(得分:0)

我应该返回 formatStrings 函数,如下所示。

function formatDuration (seconds) {
  // Complete this function
  var minutes = Math.round(seconds / 60);
  var thisSeconds = Math.round(seconds % 60);
  var hours = Math.round(seconds / 3600);
  var days = Math.round(seconds / 86400);
  var years = Math.round(seconds / 31536000);
  var stringYear = "year";
  var stringDay = "day";
  var stringHour = "hour";
  var stringMinute = "minute";
  var stringSecond = "second";

  if (years > 1) {
    stringYear = "years";  
  } else if (days > 1) {
    stringDay = "days";
  } else if (hours > 1) {
    stringHour = "hours";
  } else if (thisSeconds > 1) {
    stringSecond = "seconds";  
  } else if (minutes > 1) {
    stringMinute = "minutes"; 
  }

  if (minutes >= 60) {
    hours = Math.round(minutes/60);
    minutes = Math.abs(minutes - hours*60);
    if (minutes > 1) {
      stringMinute = "minutes"; 
    }
  } 

  if (hours >= 24) {
    hours = hours - 24;  
  }

  if (days > 365) {
    days = Math.abs(days - years*365);
  }

  var obj = new Map();
  obj.set(stringYear, years);
  obj.set(stringDay, days);
  obj.set(stringHour, hours);
  obj.set(stringMinute, minutes);
  obj.set(stringSecond, thisSeconds);

  var empArr = [];

  obj.forEach(function (value, key, mapObj) {

    if (value == 0) {
      obj.delete(key);   
    }

    var res = value + " " + key;

    if (value > 0) {
      empArr.push(res);
      formatStrings(empArr);
    }



  }); 

  return formatStrings(empArr);

}

function formatStrings(arr) {
    return arr.length == 1 ? arr[0] : arr.slice(0, arr.length - 1).join(", ") + " and " + arr[arr.length - 1];
    //console.log(str);
}


formatDuration(3600);