Calling a function multiple times in JavaScript!


  • Hi!

    Well, I need to make a function which will calculate the sum of the arguments when i call it like this.


    And it returns the sum of 10, 5, 4, 25.

    Thanks in advance!

    Thursday, January 24, 2013 7:15 AM

All replies

  • I'm not exactly sure what you are looking for, but JavaScript will allow you to call a function with a variable number of arguments.  If I remember correctly, Microsoft Evangelist Jeremy Foster explains how to call a JS Function with multiple arguments in the JavaScript Core Capabilities Module of this class:
    "Developing in HTML5 with JavaScript and CSS3 Jump Start"

    At this current time, I think the class is !*free*!.  If you are new to JavaScript, I highly encourage you to take this beneficial and fast paced class.  



    • Edited by Shawn Eary Friday, January 25, 2013 9:15 PM
    Friday, January 25, 2013 9:00 PM
  • You need to do something like Shawn suggests.

    Anyway, just for fun, this function accepts numbers, numbers as strings, zero, zero as string and rejects other strings, other types, null, undefined and empty strings.

    function addArgs(a,b,c,d){
     var i=0, j=arguments.length, add=0, checkArgs="No numbers entered";

    Call examples:

    var sum1=addArgs(10,5,4,25);
    var sum2=addArgs("10","5","4","25");
    var sum3=addArgs(10,"5","abc"25);
    var sum4=addArgs(10,null,undefined,"25");
    var sum5=addArgs("abc",null,undefined,"");
    var sum6=addArgs("","","","");
    var sum7=addArgs();
    var sum8=addArgs(0,0,0,0);
    var sum9=addArgs("0","0","0","0");
    var sum10=addArgs("0","00","000","0000");

    document.write(sum1);//Output  -> 44
    document.write(sum2);//Output  -> 44
    document.write(sum3);//Output  -> 40
    document.write(sum4);//Output  -> 35
    document.write(sum5);//Output  -> "No numbers entered"
    document.write(sum6);//Output  -> "No numbers entered"
    document.write(sum7);//Output  -> "No numbers entered"
    document.write(sum8);//Output  -> 0
    document.write(sum9);//Output  -> 0
    document.write(sum10);//Output -> 0

    Full example with this function is below. Please note that input values are returned as strings. If you want to experiment with other types or null or undefined, you will have to directly modify the source below.

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Add Arguments</title>
    <style type="text/css">
    body {width:100%;background:#ccc;color:#000;margin:0;padding:0;font:16px/18px Arial;}
    #cDiv {width:375px;margin:0;padding:20px;background:#999;color:#111;text-align:left;}
    label {display:inline-block;width:200px;}
    input {width:135px;padding:0 3px;text-align:right;}
    button {width:200px;text-align:center;margin:0 0 0 40px;}
    <script type="text/javascript">
    function addArgs(a,b,c,d){
     var i=0, j=arguments.length, add=0, checkArgs="No numbers entered";
    function writeSum(){
     var arg1=document.getElementById("inp1").value, arg2=document.getElementById("inp2").value, arg3=document.getElementById("inp3").value, arg4=document.getElementById("inp4").value;
    function resetInputs(){
     var i=0, el;
    function fx(){
     <div id="cDiv">
      <p><label for="inp1">Enter First Number</label><input type="text" id="inp1" name="inp1"></input></p>
      <p><label for="inp2">Enter Second Number</label><input type="text" id="inp2" name="inp2"></input></p>
      <p><label for="inp3">Enter Third Number</label><input type="text" id="inp3" name="inp3"></input></p>
      <p><label for="inp4">Enter Fourth Number</label><input type="text" id="inp4" name="inp4"></input></p>
      <p><button type="button" id="but1">Click to Add Numbers</button></p>
      <p><button type="button" id="but2">Click to Clear Values</button></p>
      <p><label for="inp5">Output</label><input type="text" id="inp5" name="inp5"></input></p>

    • Proposed as answer by Gopi S V Monday, March 11, 2013 7:34 AM
    Saturday, January 26, 2013 3:54 AM