use anonymous function for other function's parameter in javascript -
use anonymous function for other function's parameter in javascript -
i'm beginner on javascript. please watch code.
function map(func, ary) { var =0; var array1 = ary; while(ary[i]!=null){ array1[i] = func(ary[i]); i++; } document.write('[' + array1[0] +',' +array1[1]+']'); //just check result } map(function(x) {return (x*4)/2;},[1,3,5,7]);
i want operate "map(function(x) {return (x*4)/2;},[1,3,5,7]);
" in javascript.
how can operate code? please help me detail.
i don't understand want ask. code works. utilize null, false, true instead of null false true. , aware document.write() bad way of displaying result.
better ways are:
create/get div, span, p or elem , utilize this:div.innerhtml = result;
console.log(result);
// press f12 open firebug/web tools .
// map function returns result: function map(func, ary) { var = 0; var result = []; // empty array results while(ary[i] != null){ result[i] = func(ary[i]); i++; } homecoming result; // must homecoming result } // objects {} , arrays[] passed reference functions function map(func, ary) { var = 0; while(ary[i] != null){ ary[i] = func(ary[i]); // changing original `ary` i++; } // no need homecoming }
javascript anonymous-function
Comments
Post a Comment