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 15 of 15
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts

    Calling functions in Javascript

    Hello all here is a more intelligent question. This one is just on an exercise I am doing.

    I have to call a function using parameter values.

    my main function is this

    Code:
    function showResults(race,name,party,votes)
    
    or just
    
    function showResults
    in the next lines below

    i have to call the function

    is this the right way


    Code:
    <script type="text/javascript">
    	{
    showResults=("race[0],name1,party1,votes1");
    
        }
    </script>
    or should it be without the { and }

    like this?

    Code:
    script type="text/javascript">
    	
    showResults=("race[0],name1,party1,votes1");
    
        
    </script>
    or is it like this

    Code:
    script type="text/javascript">
    	
    showResults(race[0],name1,party1,votes1);
    
        
    </script>
    Thanks all, what would be the proper method

    Thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,159
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Quote Originally Posted by worldtraveller View Post
    or is it like this

    Code:
    script type="text/javascript">
    	
    showResults(race[0],name1,party1,votes1);
    
        
    </script>

    Thanks
    Yes. Items in quotes are literals.

    var x = "myname" // assigns the literal value myname to the variable x.
    var x = myname // assigns the current value of the variable myname to the variable x.

    You wish to pass the values of the four variables race[0],name1,party1,votes1 to your function. race[0] represents the first index value in the array named race.

    The braces { and } are used to surround the commands that are to be run as a block such as functions, for loops, if statements etc., or an object literal such as var myObject = {}

  • Users who have thanked Philip M for this post:

    worldtraveller (11-21-2010)

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    Thank you so much,
    would this be properly written for this format of Call Function

    Code:
    <script type="text/javascript">
    	
    showResults("race[0]","name1","party1","votes");
    showResults("race[1]","name2","party2","votes2");
    showResults("race[2]","name3","party3","votes3");
    showResults("race[3]","name4","party4","votes4");
    showResults("race[4]","name5","party5","votes5");
      
    </script>
    thank you

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    HOW can you READ what Philip wrote and then completely ignore him??????????

    or is it like this
    script type="text/javascript">
    showResults(race[0],name1,party1,votes1);
    </script>
    Yes.
    Do you see any quote marks in there? The version that Philip told you clearly is the correct one??? Or did you somehow read "Yes" to mean "No"??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    ok so then now i have

    Code:
    <script type="text/javascript">
    	
    showResults(race[0],name1,party1,votes);
    showResults(race[1],name2,party2,votes2);
    showResults(race[2],name3,party3,votes3);
    showResults(race[3],name4,party4,votes4);
    showResults(race[4],name5,party5,votes5);
      
    </script>
    correct i assume

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    correct i assume
    The syntax is correct.

    Whether or not the semantics are correct is another question.

    I find it strange that you would have an array for the race but then individual variable name for the other values. Why not 4 arrays?

    But yes, the syntax is correct.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    The exercise i am working on says this.

    " call the showResults() function using race[0], name1,party1 and votes1 as parameter values, repeat the previous command for the remaing four races useing race[1] through race [4]. as parameter value, for race parameter, party2 through party5 for party parameter....etc"

    so what would make more sense on this one?

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    Okay...if that's what it says. That's what you do.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    worldtraveller (11-21-2010)

  • #9
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    seems like it now i have to debug the program.
    i post a second reply about firebug

  • #10
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    I am getting a firebug message for this line


    Code:
    var item = Math.round((item/sum)*100); // script element to get the percentage
    a missing ; before statement

    but i have a ; in it
    what could be the situation?

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    Probably from the line or lines above. That line looks fine.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    worldtraveller (11-22-2010)

  • #12
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    Well here is the full function i created. I was working this with someone else.

    I just keep getting an error of saying i need to input a ; somewhere

    from this code.

    Code:
    function totalVotes(votes){ // script element that will calculate the array
         var total = 0;
         for (var i = 0; i < votes.length; i++)
              {
                 total = total + votes[i];
              }
         return total;
    }
    
    function calcPercent(item,sum) { // script element to calculate the percentage which is rounded to the nearest integer
         var item = Math.round((item/sum)*100); // script element to get the percentage
    return item;
    }
         calcPercent();
    Can anyone first hand spot if i missed a ; anywhere?

  • #13
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,227
    Thanks
    15
    Thanked 253 Times in 253 Posts
    This may be confusing things.

    var item = Math.round((item/sum)*100);
    maybe change to
    var items = Math.round((item/sum)*100); and return items

  • Users who have thanked DrDOS for this post:

    worldtraveller (11-22-2010)

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    Well *THIS* is clearly an error:
    Code:
         calcPercent();
    The function requires two arguments. You are passing none.

    SO of course it goes *KABLOOEY*.

    Try doing
    Code:
        calcPercent( 1, 3 );
    and see what you get.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #15
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    Thanks for the tip, well made some changes as recommended, so far no results yet, i may have made more mistakes in rest of my file, i will look over the other arrays loops and functions i will have more questions again


  •  

    Tags for this Thread

    Posting Permissions

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