给定一个混合元素数组,我的函数需要返回该数组中的最小数字。当数组中不存在数字时,函数应该返回'0'
要求:
- 如果给定的数组是空的,它应该返回0。
- 如果数组不包含的数字,它应该返回0。
我失败在数组不包含数字的情况下,通过测试应该返回0的测试。这里是我的代码:
function findSmallestNumberAmongMixedElements(arr) {
// your code here
if (arr.length === 0) return 0;
var shortest = arr.filter(function(a, b) {
return typeof a === 'number' && a - b;
});
return Math.min.apply(null, shortest);
}
var output = findSmallestNumberAmongMixedElements(['string', 'string']);
console.log(output); //=> Infinity (expected 0)
请添加一些数据和想要的结果。 –
请参阅http://stackoverflow.com/questions/42846325/function-getlargestelement-how-to-get-this-working-with-an-array-full-of-neg-n/42846433 - 交换<运算符。 –
检查[this](http://stackoverflow.com/a/42846435/4543207)up – Redu