人类可读递归函数的秒数

时间:2017-09-01 20:59:16

标签: javascript recursion time

我在网上看到一个问题需要一个程序将数字秒转换为秒,分钟,小时,天,年,我能够在下面提出这个功能

var arrTime = [];
function formatD(seconds){
    if(seconds > 0){
        if(seconds >= 1 && seconds <= 59){
            arrTime.push(seconds === 1 ? "1 second" : seconds + " seconds");
            return arrTime;
        }else
        if(seconds >= 60 && seconds <= 3599){
            arrTime.push(seconds === 60 ? "1 minute" : Math.floor(seconds/60) + " minutes");
            formatD(seconds - (Math.floor(seconds/60) * 60));
        }else
        if(seconds >= 3600 && seconds <= 86399){
            arrTime.push(seconds === 3600 ? "1 hour" : Math.floor(seconds/3600) + " hours");   
            formatD(seconds - (Math.floor(seconds/3600) * 3600))    
        }else
        if(seconds >= 86400 && seconds <= 31535999){
            arrTime.push(seconds === 86400 ? "1 day" : Math.floor(seconds/86400) + " hours");   
            formatD(seconds - (Math.floor(seconds/86400) * 86400))      
        }else{
            arrTime.push(seconds === 31536000 ? "1 year" : Math.floor(seconds/31536000) + " years");   
            formatD(seconds - (Math.floor(seconds/31536000) * 31536000))            
        }
    }
}

我已经研究了我的代码和我的基本递归知识,我认为这将返回一个包含结果的数组,例如,如果我输入

formatD(63)

我希望看到:

["1 minute", "3 seconds"]

但是我一直没有得到任何回报价值。在这里修理或做错了什么?

3 个答案:

答案 0 :(得分:1)

只需检查您的arrTime数组即可。

并且还从您编写的方法返回arrTime

那应该包含时间。

&#13;
&#13;
var arrTime = new Array();
function formatD(seconds){
    if(seconds > 0){
        if(seconds >= 1 && seconds <= 59){
            arrTime.push(seconds === 1 ? "1 second" : seconds + " seconds");
            return arrTime;
        }else if(seconds >= 60 && seconds <= 3599){
            arrTime.push(seconds === 60 ? "1 minute" : Math.floor(seconds/60) + " minutes");
            formatD(seconds - (Math.floor(seconds/60) * 60));
        }else if(seconds >= 3600 && seconds <= 86399){
            arrTime.push(seconds === 3600 ? "1 hour" : Math.floor(seconds/3600) + " hours");   
            formatD(seconds - (Math.floor(seconds/3600) * 3600))    
        }else if(seconds >= 86400 && seconds <= 31535999){
            arrTime.push(seconds === 86400 ? "1 day" : Math.floor(seconds/86400) + " hours");   
            formatD(seconds - (Math.floor(seconds/86400) * 86400))      
        }else{
            arrTime.push(seconds === 31536000 ? "1 year" : Math.floor(seconds/31536000) + " years");   
            formatD(seconds - (Math.floor(seconds/31536000) * 31536000))            
        }
    }

   return arrTime;
}
console.log(formatD(65));
console.log(arrTime);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以将Array#reduce与两个数组一起用于单位和相应的秒值。

要获得多个单词,您可以使用包含单词和其他计数的单词的对象。

&#13;
&#13;
function formatD(seconds) {
    function getPlural(number, word) {
        return number === 1 && word.one || word.other;
    }

    var words = {
            year: { one: 'year', other: 'years' },
            day: { one: 'day', other: 'days' },
            hour: { one: 'hour', other: 'hours' },
            minute: { one: 'minute', other: 'minutes' },
            second: { one: 'second', other: 'seconds' }
        },
        units = ['year', 'day', 'hour', 'minute', 'second'],
        values = [365 * 24 * 60 * 60, 24 * 60 * 60, 60 * 60, 60, 1],
        result = [];

    units.reduce(function (s, u, i) {
        var temp = Math.floor(s / values[i]);
        if (temp) {
            result.push(temp + ' ' + getPlural(temp, words[u]));
        }
        return s % values[i];
    }, seconds);
    return result.join(' ');
}

console.log(formatD(63));
console.log(formatD(3600));
console.log(formatD(31536000));
console.log(formatD(31626102));
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 2 :(得分:1)

&#13;
&#13;
var n = 10000;
var hourre = n%3600;
n = n-hourre;
var hours = n;
n = hourre;
var minutesre = n%60;
var minutes = n - minutesre;
seconds = n- minutes;
n = n - seconds
alert( "hours:"+hours/3600+"minutes:"+minutes/60+"seconds:"+seconds);
&#13;
&#13;
&#13;

我认为这是正确的......%得到了分裂的其余部分。