Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: JS Reduce

  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    2
    Thanked 0 Times in 0 Posts

    JS Reduce

    I need to implement a function called map (it needs to be written using reduce) in the below code that takes an array (arr) and a function (iter) as its arguments and returns an array with all its items transformed by iter, e.g. map([1, 2, 3, 4, 5], function(x) {return x * 2;}) should return [2, 4, 6, 8, 10]

    PHP Code:
    (function(){
        var 
    reduce = function(arriteracc){
            var 
    len arr.length0;
            for (
    0len+= 1acc iter(accarr[i], i);
            return 
    acc;
        };
        var 
    map = function(arriter){
            
    // Implementation code  should go here
            //
            //
        
    };
        return 
    map([12345], function(x){return 2;});
    })(); 
    Can anyone please help me?

    Thanks

  • #2
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Code:
    Array.prototype.map = function(fnc) {
    var a = new Array(this.length);
    for (var i = 0; i < this.length; i++) {
        a[i] = fnc(this[i]);
    }
    return a;
    }
    Code:
    alert([1,2,3,4].map(function(x){ return x * 2}));

  • Users who have thanked johnmerlino for this post:

    matrixstar (01-03-2011)

  • #3
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    Or
    Code:
    <script type="text/javascript">
    Array.prototype.map = function(func) {
      for(var a = new Array(), i = 0; i < this.length; a[i] = func(this[i++]));
      return a;
    };
    alert([1,2,3,4].map(function(x){ return x * 2}));
    </script>
    Last edited by Nile; 01-03-2011 at 07:29 PM.

  • Users who have thanked Nile for this post:

    matrixstar (01-03-2011)

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    MDN uses this code
    PHP Code:
    if (!Array.prototype.map)
    {
      Array.
    prototype.map = function(fun /*, thisp */)
      {
        
    "use strict";

        if (
    this === void 0 || this === null)
          throw new 
    TypeError();

        var 
    Object(this);
        var 
    len t.length >>> 0;
        if (
    typeof fun !== "function")
          throw new 
    TypeError();

        var 
    res = new Array(len);
        var 
    thisp arguments[1];
        for (var 
    0leni++)
        {
          if (
    i in t)
            
    res[i] = fun.call(thispt[i], it);
        }

        return 
    res;
      };

    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    Quote Originally Posted by johnmerlino View Post
    Code:
    Array.prototype.map = function(fnc) {
    var a = new Array(this.length);
    for (var i = 0; i < this.length; i++) {
        a[i] = fnc(this[i]);
    }
    return a;
    }
    Code:
    alert([1,2,3,4].map(function(x){ return x * 2}));
    don't use this, ever! it breaks all browser besides IE! same applies for matrixstar's code as well.

    1. it doesn't skip over undefined slots in the array, which causes out-of-spec execution of the passed method.

    2. where's the dynamic this? nomally, map function are supposed to accept this, but this function never applies a context, it inherits from wherever the [].map function is defined! yikes!

    in other words, how can one use a good re-usable map function with code like yours:

    Code:
    function gt(n){return n>this;}
    [1,2,3,4,5].map(gt, 3)
    Code:
    should return : [false, false, false, true, true]

    3. where are the other arguments? normally arguments[1] is the array index, and arguments[2] is the whole, original array.
    how can i build an indexed array, not to mention a ton of other applications, if you take away [].map's other arguments?
    Code:
    Array(11).toString().split("").map(function(a,i){return i;})



    4. since [].map is a standard, and the code doesn;t object-detect, it clobbers the native [].map which provides the functionality mentioned above. If i tried to use my re-usable this-bound functions on a site that used your code, my code would break. that's not right.

    Dormilich's code is overkill, but at least it works correctly, and who cares how long stuff takes for IE users?
    Last edited by rnd me; 01-04-2011 at 07:31 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    Quote Originally Posted by rnd me View Post
    Dormilich's code is overkill, but at least it works correctly, and who cares how long stuff takes for IE users?
    ask that the Mozilla guys, I just happened to know that they provide alternate code for JS 1.6+ methods.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    Quote Originally Posted by Dormilich View Post
    ask that the Mozilla guys, I just happened to know that they provide alternate code for JS 1.6+ methods.
    they mention explicitly that they need the full stock of the code posted for their interpreter and that not all runtimes need this much explication. IE doesn't need as much, so it's very possible to optimize the function for performance whilst maintaining full operational compatibility. I sometimes paste the mdc versions (they are in my snippits manager), but they are so bulky that i prefer other versions.


    the "F" library in my signature provides all the array methods, along with dozens of re-usable functions and global shortcuts, all in less than 2kb of code.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •