Your arr value is [1,2,[3,4]]. That's an array with three entries: the number 1, the number 2, and the array [3,4].
The first two arguments to a reduce callback are
- the accumulator (which is the return value of the previous invocation of the callback) and
- a value taken in sequence from the array.
You don't supply an initial accumulator in your code, so that means the first call to the reduce callback accepts the first two entries as arguments (i.e., the first element of the array is used as an accumulator). concat is not a function because the initial value of a is a number, not an array. You tested to see if arr was an array, but did not test to see if a was an array.
The clear solution here is to supply an initial value to use as an accumulator:
function flatten(arr){ return arr.reduce(function(a, b) { return a.concat(b); }, []); }
This creates an empty array (with []) and then supplies it as the a value for the first call to the reduce callback.
arris an array, but calling concat ona. Why don't you test to see ifais an array? :)