...

View Full Version : Calling functions in Javascript



worldtraveller
11-21-2010, 08:13 PM
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




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





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

}
</script>


or should it be without the { and }

like this?




script type="text/javascript">

showResults=("race[0],name1,party1,votes1");


</script>


or is it like this



script type="text/javascript">

showResults(race[0],name1,party1,votes1);


</script>




Thanks all, what would be the proper method

Thanks

Philip M
11-21-2010, 08:59 PM
or is it like this



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 = {}

worldtraveller
11-21-2010, 10:27 PM
Thank you so much,
would this be properly written for this format of Call Function


<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

Old Pedant
11-21-2010, 10:32 PM
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"??

worldtraveller
11-21-2010, 10:53 PM
ok so then now i have


<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

Old Pedant
11-21-2010, 10:59 PM
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.

worldtraveller
11-21-2010, 11:12 PM
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?

Old Pedant
11-21-2010, 11:52 PM
Okay...if that's what it says. That's what you do.

worldtraveller
11-21-2010, 11:58 PM
seems like it now i have to debug the program.
i post a second reply about firebug

worldtraveller
11-22-2010, 12:01 AM
I am getting a firebug message for this line



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?

Old Pedant
11-22-2010, 12:55 AM
Probably from the line or lines above. That line looks fine.

worldtraveller
11-22-2010, 02:48 AM
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.


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?

DrDOS
11-22-2010, 02:55 AM
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

Old Pedant
11-22-2010, 04:09 AM
Well *THIS* is clearly an error:


calcPercent();

The function requires two arguments. You are passing none.

SO of course it goes *KABLOOEY*.

Try doing

calcPercent( 1, 3 );

and see what you get.

worldtraveller
11-22-2010, 04:27 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum