有没有一个实例需要在JavaScript中获取reduce函数的第一个数字?
这个问题看起来像是一个骗人的问题,只是因为当我提交函数进行验证时,我得到的一个要求如下 当存在连接时,是否应返回数组中的最小元素 为什么我们应该/会检查数量上的联系有没有一个实例需要在JavaScript中获取reduce函数的第一个数字?,javascript,arrays,Javascript,Arrays,这个问题看起来像是一个骗人的问题,只是因为当我提交函数进行验证时,我得到的一个要求如下 当存在连接时,是否应返回数组中的最小元素 为什么我们应该/会检查数量上的联系 1 === 1 //returns true. 我错过什么了吗 这是实际的要求 编写一个名为FindCallestNumberMongMixedElements的函数 给定一个混合元素数组, FindCallestNumberMongMixedElements返回最小的数字 在给定数组中 注: *如果给定数组为空,则应返回0。 *
1 === 1 //returns true.
我错过什么了吗
这是实际的要求
编写一个名为FindCallestNumberMongMixedElements的函数
给定一个混合元素数组,
FindCallestNumberMongMixedElements返回最小的数字
在给定数组中
注:
*如果给定数组为空,则应返回0。
*如果数组不包含数字,则应返回0
解决方案的问题在于,包含单个数字的字符串被视为数字 以数组为例
[4, 'lincoln', 9, '3']
您的解决方案将返回“3”,这是一个错误,它应该返回4
有时,对于这样的问题,外部迭代更容易理解。对于这样的搜索,不需要使用函数式方法,特别是当它可以通过数组的一次遍历来完成时
function findSmallestNumberAmongMixedElements(arr) {
var smallest = null;
for (var i = 0; i < arr.length; i++) {
var n = arr[i];
if (typeof n == 'number') {
if (smallest === null) {
smallest = n;
} else if (n < smallest) {
smallest = n;
}
}
}
if (smallest === null) {
smallest = 0;
// This handles the case of not finding a number
// or if the array was empty.
}
return smallest;
}
console.log(findSmallestNumberAmongMixedElements([4, 'lincoln', 9, '3']));
可能的基于Array.reduce的解决方案可能类似于以下解决方案
var RejectsSmallestNumberValue=函数baseValueOf{
类型的函数值{
返回类型==null?类型:baseValueOf.calltype.valueOf;
}
函数是NumberValueType{
返回typeof type=='number';
}
返回函数拒绝收集器,项,idx,列表{
变量
值=项目的值;
如果idx==0{
collector.value=Number.POSITIVE_∞;
}
如果isNumberValuevalue{
collector.isContainsAnyNumber=true;
collector.value=Math.mincollector.value,value;
}
如果idx>=list.length-1&&!collector.isContainesAnyNumber{
collector.value=0;
}
回程收集器;
};
}Object.prototype.valueOf;
console.log[4',lincoln',9',octopus',0.5'].ReduceRejectsSmallestNumberValue…:,[4,'lincoln',9,'octopus','0.5'].还原对象最小值{
IsContainesAnyNumber:false,
数值:0
}.价值;
console.log[4',lincoln',9',octopus',0.5',1].ReduceRejectsSmallestNumberValue…:,[4,'lincoln',9,'octopus','0.5',1].ReduceRejectsSmallNumberValue{
IsContainesAnyNumber:false,
数值:0
}.价值;
console.log[14,'lincoln',9,'octopus','0.5'].ReduceRejectsSmallestNumberValue…:,[14,'lincoln',9,'octopus','0.5'].还原对象最小值{
IsContainesAnyNumber:false,
数值:0
}.价值;
console.log[14,'lincoln','octopus','0.5'].ReduceRejectsSmallestNumberValue…:,[14,'lincoln','octopus','0.5']。还原对象最小值{
IsContainesAnyNumber:false,
数值:0
}.价值;
console.log['lincoln','octopus','0.5'].ReduceRejectsSmallestNumberValue…:,['lincoln','octopus','0.5'].还原对象最小值{
IsContainesAnyNumber:false,
数值:0
}.价值;
console.log[].reducerejectSmallestNumberValue…:,[]还原对象最小数值{
IsContainesAnyNumber:false,
数值:0
}.价值;
.作为控制台包装器{max height:100%!important;top:0;}如果数组中传递了字符串,则您的方法将返回字符串,即对于[5,4],结果将是4,而不是5或4。是否有任何方法可以找出验证器使用的输入?@KarlReid对不起,您可以解释一下您的意思吗?
function findSmallestNumberAmongMixedElements(arr) {
var smallest = null;
for (var i = 0; i < arr.length; i++) {
var n = arr[i];
if (typeof n == 'number') {
if (smallest === null) {
smallest = n;
} else if (n < smallest) {
smallest = n;
}
}
}
if (smallest === null) {
smallest = 0;
// This handles the case of not finding a number
// or if the array was empty.
}
return smallest;
}
console.log(findSmallestNumberAmongMixedElements([4, 'lincoln', 9, '3']));