[英]implement array.prototype.reduce() on string
I have some confusion regarding how reduce operation takes place on a string.First a new Str instance is created and sends desired string as a parameter.我对如何对字符串进行缩减操作感到困惑。首先创建一个新的 Str 实例并将所需的字符串作为参数发送。
Then using split method it splits into an array of string.A reduceIt method takes the array and execute a reduce operation which returns the array element which has height length.然后使用 split 方法将其拆分为一个字符串数组。reduceIt 方法获取该数组并执行 reduce 操作,该操作返回具有高度长度的数组元素。
It works fine with a two element array.But if there is more than two elements it returns NAN.它适用于两个元素的数组。但是如果有两个以上的元素,它返回 NAN。
Why it returns NAN for array having more than two elements??为什么它为具有两个以上元素的数组返回 NAN?
function Str(text){
this.text=text.split('\n');
}
Str.prototype.reduceIt = function() {
return this.text.reduce(function(first,last,index,arr) {
return Math.max(first.length,last.length);
});
};
var t=new Str('i am a boy\n she loves cats\n a goat ate my flower garden ');
console.log(t.reduceIt());
The first time the callback is called first
is a string (the first element in the array), and your function makes sense when first
and last
are both strings, so it works when the callback is only called once (the array has at most 2 elements). 第一次调用回调
first
是一个字符串(数组中的第一个元素),以及你的函数是有道理的,当first
和last
都是字符串,所以它的工作原理,当回调时才调用(数组至多2元素)。
The second time it is called it is the result of the previous call, a number
. 第二次调用是上次调用的结果,即
number
。 When you call first.length
on a number you get undefined
and when you call Math.max
on that you get NaN
. 当您在数字上调用
first.length
,您将得到undefined
而当您在Math.max
上调用时,您将得到NaN
。
If you want to find the length of the longest string in your array, you could use: 如果要查找数组中最长字符串的长度,可以使用:
Math.max.apply(Math, this.text.map(function (str) { return str.length; }));
Some good answers already. 已经有一些好的答案。 :-)
:-)
The simple way to fix your problem is to supply an initial value of 0, then compare the returned value with the length of the new string, so: 解决问题的简单方法是提供一个初始值0,然后将返回值与新字符串的长度进行比较,因此:
Str.prototype.reduceIt = function() {
return this.text.reduce(function(first,last,index,arr) {
// Compare current value (first) with length of string
return Math.max(first,last.length);
}, 0); // supply 0 as the initial value
};
It might make things clearer to rename first to maxSoFar and last to currentString . 这可能会使事情更清晰重命名第一个maxSoFar和最后 currentString。
Why it returns NAN for array having more than two elements??
为什么对于具有两个以上元素的数组返回NAN?
Because number.length
is undefined , let's name your function foo
and follow how it's invoked 因为
number.length
是undefined ,所以让我们将函数命名为foo
并遵循其调用方式
foo(0, "i am a boy")
gives NaN
foo(0, "i am a boy")
给出NaN
foo(NaN, " she loves cats")
gives NaN
foo(NaN, " she loves cats")
给NaN
foo(NaN, " a goat ate my flower garden ")
gives NaN
foo(NaN, " a goat ate my flower garden ")
给出了NaN
Giving a final result of NaN
. 给出
NaN
的最终结果。
This happens because number.length
is undefined and Math.max(undefined, x)
is NaN
发生这种情况是因为
number.length
是不确定的,而Math.max(undefined, x)
是NaN
It looks like you wanted to write a function which only takes the length of the second arg 看来您想编写一个仅占用第二个arg 长度的函数
function foo(a, b) {
return Math.max(a, b.length);
}
In this case, you'll get 在这种情况下,您会得到
foo(0, "i am a boy")
gives 10
foo(0, "i am a boy")
给出10
foo(10, " she loves cats")
gives 15
foo(10, " she loves cats")
给出15
foo(15, " a goat ate my flower garden ")
gives 29
foo(15, " a goat ate my flower garden ")
给29
Giving a final result of 29
. 最终结果为
29
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.