2017-06-22 47 views
1

嗨我創建一個函數,返回給定數組內的最小數字。如果數組包含沒有數字,它應該返回0。混合元素小號 - JS

這裏是我的功能:

function findSmallestNumberAmongMixedElements(arr){ 
     if(arr.length === 0 && typeof arr === 'string'){ 
     return 0; 
     } else{ 
     return Math.min.apply(null, arr); //min=1 
     } 
    } 
    var output = findSmallestNumberAmongMixedElements([4, 'lincoln', 9, 'octopus']); 
    console.log(output); // --> 4 

現在我的答案返回NAN而不是4.你有什麼想法,我究竟做錯了什麼?

回答

2

typeof arr === "string"將永遠是false如果你正在傳遞一個數組(無論數組是否包含字符串與該測試無關)。

你應該做的是首先篩選數字出用filter數組,然後調用Math.min過濾的陣列上:

function findSmallestNumberAmongMixedElements(arr) { 
 
    var onlyNumbers = arr.filter(e => typeof e === "number"); // filter out only items that are numbers 
 
    if(onlyNumbers.length === 0) return 0;     // if there is no numbers, return 0 
 
    return Math.min.apply(null, onlyNumbers);     // otherwise return the min of them 
 
} 
 

 
var output = findSmallestNumberAmongMixedElements([4, 'lincoln', 9, 'octopus']); 
 
console.log(output); // --> 4

+0

我喜歡你的解決方案,但是如果數組中沒有項目,這將不會返回0嗎? – Matt

+0

@Matt你說得對。我將爲此添加代碼。 –

2

您當前的功能可能會產生NaN你的時候嘗試將Math.min應用於包含stringnumber值的輸入數組。 Math.min只能處理數字輸入,因此string的存在可能會導致它返回NaN錯誤值。

有一個簡單的解決方案:從數組中濾除所有非number值。我們可以檢查過濾數組的長度,看看是否有非數字值,如果有的話,請不用擔心。

function findSmallestNumberAmongMixedElements(arr) { 
 
    var filtered = arr.filter(function(el) { 
 
    return typeof el == 'number'; 
 
    }); 
 
    if (filtered.length > 0) { 
 
    return Math.min.apply(Math, filtered); 
 
    } else { 
 
    return 0; 
 
    } 
 
} 
 

 
[ 
 
    [4, 'lincoln', 9, 'octopus'], // 4 
 
    [],       // 0 
 
    ['a', 3, 2],     // 2 
 
    ['h', 'i'],     // 0 
 
    ['-3', 3],     // 3 
 
].forEach(function(input) { 
 
    console.log(input, findSmallestNumberAmongMixedElements(input)); 
 
});

1

在你Math.min.apply(null, arr),你得到NaN因爲有數組中的字符串。您的typeof arr === 'string'不循環您的數組以排除數組中的字符串值

你需要做的,實現你的目標是什麼可能只是做一個for循環或forEach循環:

function findSmallestNumberAmongMixedElements(arr){ 
    var min = Infinity; 

    // if argument is not array or array has no value 
    if (arr.constructor !== Array || arr.length === 0){ 
     min = 0; 
     return min; 
    } 

    /* for loop */ 
    for (var i = 0, len = arr.length; i < len; i++){ 
     if (arr[i] === 0 || typeof arr[i] !== 'number') continue; 
     min = Math.min(min, arr[i]); 
    } 

    /* forEach loop */ 
    arr.forEach(function(value, index){ 
     if (value === 0 || typeof value !== 'number') return; 
     min = Math.min(min, value); 
    }); 

    return min; 
} 

var output = findSmallestNumberAmongMixedElements([4, 'lincoln', 9, 'octopus']); 
console.log(output); // --> 4 
1

我想你給它之前應該過濾你的陣列Math.min

請嘗試以下內容

function isNumber (obj) { 
    return obj!== undefined && typeof(obj) === 'number' && !isNaN(obj); 
} 

function smallestInMixedArray (arr) { 
    if (arr.length === 0 || !arr.filter) { 
     return 0; 
    } else { 
     arr = arr.filter(isNumber) 
     return Math.min.apply(null, arr); //min=1 
    } 
} 

var output = smallestInMixedArray([4, 'lincoln', 9, 'octopus']); 
console.log(output); // --> 4