2017-07-11 226 views
0

我在其中一個網站上解算算法,但我實際上無法測試我的代碼,因爲控制檯顯示未定義,雖然返回結果。請問,有人可以告訴我代碼有什麼問題嗎?控制檯日誌雖未返回結果,但返回結果

代碼

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

我找不到假設打印結果的日誌,我發現唯一的返回語句是'formatStrings',它與您的語句相矛盾。 – ASDFGerte

+0

在你的'formatDuration'函數中返回格式化的字符串。 |此外,上下文,如果有人感興趣:https://www.codewars.com/kata/human-readable-duration-format/train/javascript – yuriy636

+0

你記錄'str'到控制檯日誌。它看起來沒有其他地方。 (並且它在返回之後;它根本不會記錄任何東西,所以我不知道你在說什麼。)'formatDuration'什麼都不返回,所以當你運行它時會得到'undefined' 。 –

回答

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);