Hello I'm working on a problem that requires me to change an set array of numbers into an array that returns the original numbers as a function. So we get a return of a2 instead of a[2].
I dont want the answer I just need a hint. I know i can loop through the array and use .pop() to get the last value of the array, but then I dont know how to convert it to a function from there. any hints?
-
1if this is homework it should be tagged as such. it is ok if it is. just make sure you tag itrlemon– rlemon2012年04月05日 03:45:27 +00:00Commented Apr 5, 2012 at 3:45
-
2and the reason for that is: when questions are tagged w/ "homework" tag, users of the site know that coding up a full solution is not as desirable as pointing you in the right directionmechanical_meat– mechanical_meat2012年04月05日 03:47:20 +00:00Commented Apr 5, 2012 at 3:47
-
1Good point, I didn't really consider that. Thanks for the tip, hopefully my answer isn't going to far and is adding some knowledge! (let me know if it isn't)Will Buck– Will Buck2012年04月05日 03:51:50 +00:00Commented Apr 5, 2012 at 3:51
3 Answers 3
var numToFun = [1, 2, 3];
var numToFunLength = numToFun.length;
for (var i = 0; i < numToFunLength; i++) {
(function(num){
numToFun.unshift(function() {
return num;
});
}(numToFun.pop()))
}
basically it pops out a number from the last, builds a function with that number returned, and put back into the first of the array. after one full cycle, all of them are functions.
here's the catch: how this works, it's up to you to research
why the loop does not look like the straightforward pop-unshift:
for (var i = 0; i < numToFunLength; i++) {
numToFun.unshift(function() { //put into first a function
return numToFun.pop() //that returns a number
});
}
and why i did this: (HINT: performance)
var numToFunLength = numToFun.length;
Comments
There's three important steps here:
Extract the number value from the array. Within a loop with an iterator of
i, it might look like this:var num = numArray[i];This is important, because
iwill not retain its value that it had when you created the new function - it'll end up with the last value it had, once theforloop is finished. The function itself might look like this:function() { return num; }There's no reference to
iany more, which is important - to understand better, read about closures. The final step would be to add the new function to the array of functions that you want.
...and you're done!
1 Comment
EDIT: See other's answers for good explanations of how to do this right, I will fix mine also though
As others have pointed out, one of the tricky things in javascript that many struggle with (myself included, obviously) is that scoping variables in javascript is dissimilar to many other languages; scopes are almost purely defined by functions, not the {} blocks of, for example, a for loop, as java/C would be.
So, below you can see (and in other answers here) a scoping function can aid with such a problem.
var numArray = [12, 33, 55];
var funcArray = [];
var numArrLength = numArray.length; // Don't do this in for loop to avoid the check multiple times
for(var j=0; j < numArrLength; j++) {
var scopeMe = function() {
var numToReturn = numArray[j];
console.log('now loading... ' + numToReturn);
var newFunc = function() {
return numToReturn;
};
return newFunc;
}();
funcArray.push(scopeMe);
};
console.log('now me');
console.log(funcArray);
console.log(funcArray[0]());
console.log(funcArray[1]());
console.log(funcArray[2]());
console.log(funcArray[1]()); // To ensure it's repeatable
EDIT my old bad answer below
What you'll want to do is something like
var funcArray = [];
for(...) {
var newFunc = function() {
return numArray.pop();
}
funcArray.push(newFunc);
}
The key here is that functions in javascript can be named variables, and passed around as such :)
7 Comments
i will be outside the newFunc closure, and return the last item every time.