I'm puzzled about how the reduce operation is carried out on a string. Initially, a new Str instance is created with the desired string as a parameter.
Following that, the split method is used to divide the string into an array of strings. A method called reduceIt takes this array and performs a reduce operation which returns the element in the array with the longest length.
Everything works well when dealing with a two-element array. However, if there are more than two elements, it ends up returning NAN.
What causes it to return NAN for arrays with more than two elements?
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());