Your issue is that you forgot to pass in the initialValue
argument to the reduce function, which must be an array in this case.
var r = i.reduce(function(a, b) {
return a.concat(b);
}, []);
Without providing an initialValue
, the a
value for the first call will be the first element in the i
array, which is the string big in your case, so you will be calling the String.prototype.concat
function instead of Array.prototype.concat
.
That means at the end, r
is a string and strings don't have a reduce
function.
Your solution could be simplified however:
['big',[0,1,2,3],'tiny'].reduce(function longest(a, b) {
b = Array.isArray(b)? b.reduce(longest, '') : b;
return b.length > a.length? b : a;
}, '');