I am just learning how to use higher order JS functions (map, forEach, reduce, etc.) and stumbled into confusion. I am trying to write a simple "range" function, but it doesn't seem to populate my output array. This is the goal:
range(1, 4) // [1, 2, 3, 4]
I get this:
[undefined × 4]
Here is my code:
function range(num1, num2) { var rangeArr = new Array((num2 + 1) - num1); return rangeArr.map(function(e, i, arr) {return arr[i] = num1 + i}); }
What am I missing here? As far as I can tell, the problem seems to be related to how I use the "new array", but beyond that I am lost.
Oh, and here is the part that really bothers me. This works great:
function bleck() { var blah = [1, 2, 3, 4]; var x = 'wtf'; return blah.map(function(e, i, arr) {return arr[i] = x}) } ["wtf", "wtf", "wtf", "wtf"]
Thanks!!
source share