|
-
Calling Functions
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
-
Only the latter is JavaScript.
Code:
function test(par1,par2) {
// Declaring
}
test("hello","world"); // Calling
Great wit and madness are near allied, and fine a line their bounds divide.
-
Thank you
would this be proper written format?
for Calling 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>
thanks
-
You need to do a bit of reading up. There is a lot to learn about functions.
For starters you could try googling 'javascript functions', 'javascript reference and primitive types' and 'javascript variable scope'.
A few links I found after a quick search
http://www.quirksmode.org/js/function.html
https://developer.mozilla.org/en/Jav...function_scope
http://www.howtocreate.co.uk/tutoria...ript/functions
http://docstore.mik.ua/orelly/webpro...pt/ch04_04.htm
http://docstore.mik.ua/orelly/webpro...pt/ch11_02.htm
Looking at your example
Code:
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");
One additional goal would be to look at a way of reducing the repetition in your script. Maybe using a loop for example?
PHP Code:
var showResults = function(race, name, party, votes){
// just testing
// console.log needs Google Chrome or firefox and the firebug plug-in.
console.log (race + ' - ' + name + ' - ' + party + ' - ' + votes);
}
var i = 0;
var results = 5;
var x;
for (i = 0; i < results; i+=1){
x = i + 1;
showResults("race["+ i +"]", "name" + x, "party" + x, "votes" + x);
}
/*
Output from showResults test
race[0] - name1 - party1 - votes1
race[1] - name2 - party2 - votes2
race[2] - name3 - party3 - votes3
race[3] - name4 - party4 - votes4
race[4] - name5 - party5 - votes5
*/
RLM
-
I made a mistake with my call function
it should be like this
Code:
<script type="text/javascript">
showResults(race[0],name1,party1,votes1);
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>
Well for the exercise i was supposed to be inputed in this manner, so far my entire program is not working though
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
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
-
Forum Rules
|
|
Bookmarks