嗨我創建一個函數,返回給定數組內的最小數字。如果數組包含沒有數字,它應該返回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.你有什麼想法,我究竟做錯了什麼?
我喜歡你的解決方案,但是如果數組中沒有項目,這將不會返回0嗎? – Matt
@Matt你說得對。我將爲此添加代碼。 –