/    Sign up×
Community /Pin to ProfileBookmark

Functions and placements of {

I just did this function in my program

I am wondering if i wrote it right?

such as the placements of my { and }

did i put them in the correct spots?

thanks

here is my 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;
[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@rpg2009Nov 22.2010 — Not being funny, but did you read any of those links I posted for you on functions?

Your code.
[code=php]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;
[/code]


Well for starters

[CODE]for (var i = 0; i < votes.length; i++);[/CODE]

Note the semi-colon at the end. You don't want this as your following block {....} will never be executed.

If you're going for a....

[CODE]blah blah(){
.....
.....
{[/CODE]


format then I'd say keep the rest of your code in the same format.

You're missing a closing brace at the end.

Are you using an editor like notepad++ ?

If you click on an opening brace or curly bracket { it will highlite in red where the closing one } is.

So it should be
[code=php]function totalVotes(votes){
var total = 0;
for (var i = 0; i < votes.length; i++){ // removed semicolon
total = total + votes[i];
} // note lines up with 'for'
return total;
} // added the closing brace. Note lines up with the 'function'[/code]


RPG
Copy linkTweet thisAlerts:
@worldtravellerauthorNov 23.2010 — Thank you so much
Copy linkTweet thisAlerts:
@rpg2009Nov 23.2010 — You're welcome?
Copy linkTweet thisAlerts:
@worldtravellerauthorNov 23.2010 — now i have to figure how to make rest of the program to work!
×

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,
)...