Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

JavaScript function to add an idefinite amount of numbers

  • 06-06-2013 10:55PM
    #1
    Registered Users, Registered Users 2 Posts: 13


    I'm a newbie to programming and I'm trying to make a recursive function that will add an indefinite amount of numbers. The code below shows the kind of thing I'm trying. I know it's not right because 'arguments' isn't strictly an array so you can't use 'shift()' with it.

    Is there some way to get it to work in the way I want?

    function adder() {
           
        if (arguments.length == 1) {return arguments[0];}
        else {return arguments[0] + adder(arguments.shift());}
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 13 Naked In Public


    I know how to do it iteratively but is it possible to do recursively?
    function adder() {
           
        var sum = 0;   
           
        for (var i=0; i<arguments.length; i++) {
            
            sum += arguments[i];
        }
        
        return sum;
    }
    


  • Registered Users, Registered Users 2 Posts: 13 Naked In Public


    What if I pass the values as an array and then use 'shift()' on the element 'argument[0]' instead the whole arguments object?


  • Registered Users, Registered Users 2 Posts: 13 Naked In Public


    I just tried this code:
    function adder() {
           
        if (arguments[0].length == 1) {return arguments[0][0];}
        else {return arguments[0][0] + adder(arguments[0].shift());}
    }
    
    It works if I pass it an array with one element. Any bigger and nothing happens.


  • Registered Users, Registered Users 2 Posts: 13 Naked In Public


    I tried replacing the recursive mention of 'adder' with 'arguments.callee', but still no joy.
    function adder() {
           
        if (arguments[0].length == 1) {return arguments[0][0];}
        else {return arguments[0][0] + arguments.callee(arguments[0].shift());}
    }
    


  • Registered Users, Registered Users 2 Posts: 13 Naked In Public


    Ah, wait. Using arrays isn't the way I want to to go. I want the function to be able to operate on an arbitrary number of inputs.


  • Advertisement
  • Closed Accounts Posts: 7,144 ✭✭✭DonkeyStyle \o/


    Here's my effort, I dunno if it's so expensive that it defeats the point. And I wouldn't call it arbitrary exactly, since the stack will shit itself trying to add 50,000 numbers like this (where a simple loop seems happy).

    [HTML]<html><head></head><body>

    <script type="text/javascript">

    alert(adder(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15));

    function adder() {

    // Convert arguments to array/object or copy if already an array
    if(typeof arguments[0] == 'number') {
    var args = Array.prototype.slice.call(arguments);
    } else {
    var args = arguments[0];
    }

    //console.log(args);

    // Adding stuff
    if(args.length == 1) {
    return args[0];
    } else {
    args[0] += args.pop();
    return adder(args);
    }

    }

    </script>
    </body></html>[/HTML]


  • Registered Users, Registered Users 2 Posts: 13 Naked In Public


    Is it possible to cut the first argument from the arguments object and then pass it on?


Advertisement