/    Sign up×
Community /Pin to ProfileBookmark

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

[/CODE]

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>
[/CODE]

or should it be without the { and }

like this?

[CODE]

script type=”text/javascript”>

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

</script>
[/CODE]

or is it like this

[CODE]
script type=”text/javascript”>

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

</script>

[/CODE]

Thanks all, what would be the proper method

Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Nov 21.2010 — Only the latter is JavaScript.function test(par1,par2) {
// Declaring
}
test("hello","world"); // Calling
Copy linkTweet thisAlerts:
@worldtravellerauthorNov 21.2010 — 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>
[/CODE]


thanks
Copy linkTweet thisAlerts:
@rpg2009Nov 22.2010 — 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/JavaScript/Reference/Functions_and_function_scope

http://www.howtocreate.co.uk/tutorials/javascript/functions

http://docstore.mik.ua/orelly/webprog/jscript/ch04_04.htm

http://docstore.mik.ua/orelly/webprog/jscript/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");[/CODE]


One additional goal would be to look at a way of reducing the repetition in your script. Maybe using a loop for example?

[code=php]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
*/[/code]


RLM
Copy linkTweet thisAlerts:
@worldtravellerauthorNov 22.2010 — 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>[/CODE]


Well for the exercise i was supposed to be inputed in this manner, so far my entire program is not working though
×

Success!

Help @worldtraveller spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...