Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

JavaScript function to add an idefinite amount of numbers

  • 06-06-2013 9: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,145 ✭✭✭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