/    Sign up×
Community /Pin to ProfileBookmark

A cleaner way of doing multiple Logical Operators?

Hi All – this is more of a best practise question rather than a “How do I do…” – I have a form that submits to a function for evaluation to produce a result that shows or hides a div based on the result. The form is simply four drop downs.

Obviously I will use logical operators to make a decision, but is there a better way of writing the length number of combinations? ie

if (thisform.dropDown1.selectedIndex == 1 &&
thisform.dropDown2.selectedIndex == 1 &&
thisform.dropDown3.selectedIndex == 1 &&
thisform.dropDown4.selectedIndex == 1 &&
) {
//do whatever
}

it just seems I will have a stack of if statements in my .js file.

Just had a thought – would gathering the results first into variable – such as var count = thisform.dropDown1.selectedIndex;
var count += thisform.dropDown2.selectedIndex; etc

Then do a switch() on the count var? That might work; can anyone suggest a better way of doing this?

thanks

frank

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@justinbarneskinAug 25.2009 — function abc(){ var yes=1;

for(i=0;i<4;i++){if(document.getElementById('dropDown'+i).selectedIndex!=1){yes=0}}

if(yes){doIt()}

}
Copy linkTweet thisAlerts:
@rnd_meAug 25.2009 — if you just added them, 0 and 2 add the same as two ones.

you can at least shorten the code, if not use a loop:

[CODE]var tf=thisform, dd="dropDown", si="selectedIndex";
if (
tf[dd+1][si] == 1 &&
tf[dd+2][si] == 1 &&
tf[dd+3][si] == 1 &&
tf[dd+4][si] == 1 &&
) {
//do whatever
}
[/CODE]
×

Success!

Help @bradfrankshaw 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.27,
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,
)...