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.
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.
It works fine with a two element array.But if there is more than two elements it returns NAN.
Why it returns NAN for array having 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());