嗨我创建一个函数,返回给定数组内的最小数字。如果数组包含没有数字,它应该返回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你说得对。我将为此添加代码。 –