I'm quite new to JavaScript and don't understand a few of its behaviours. I want to write a recursive version of reduce function found in Eloquent JavaScript book. That's my code:
function rec_reduce( fn, base, list ) {
if( list.length === 0 ) {
return base;
}
else {
rec_reduce( fn, fn( base, list[ 0 ] ), list.slice( 1 ) );
}
}
print( rec_reduce( Math.min, 100, [ 5, 3, 7, 2, 6, 5 ] ));
The result was:
undefined
To see what's going on I put:
print( base );
as a first line of the function and the result was:
100
5
3
3
2
2
2
undefined
Whould anyone explain me why?