Strings and OR

R

Roy Reed

Hi

What's the correct syntax for 'or' for selecting any of a group of countries
from a drop-down list?

This is what I thought should work, but it doesn't seem to.

if (form.country.options[chooseSelect].value == "FR" || "DE" || "UK") {
do this
} else {
do that
}

Thanks for any help.

Roy
 
I

Ivan Marsh

Hi

What's the correct syntax for 'or' for selecting any of a group of
countries from a drop-down list?

This is what I thought should work, but it doesn't seem to.

if (form.country.options[chooseSelect].value == "FR" || "DE" || "UK") {
do this
} else {
do that
}

Thanks for any help.

Roy

Hey Roy,

What you've got there is a single comparison of
form.country.options[chooseSelect].value and two OR conditions that will
always evaluate to true.

Try:

if (form.country.options[chooseSelect].value == "FR" ||
form.country.options[chooseSelect].value == "DE" ||
form.country.options[chooseSelect].value == "UK") {
 
R

Roy Reed

Ivan Marsh said:
Hi

What's the correct syntax for 'or' for selecting any of a group of
countries from a drop-down list?

This is what I thought should work, but it doesn't seem to.

if (form.country.options[chooseSelect].value == "FR" || "DE" || "UK") {
do this
} else {
do that
}

Thanks for any help.

Roy

Hey Roy,

What you've got there is a single comparison of
form.country.options[chooseSelect].value and two OR conditions that will
always evaluate to true.

Try:

if (form.country.options[chooseSelect].value == "FR" ||
form.country.options[chooseSelect].value == "DE" ||
form.country.options[chooseSelect].value == "UK") {

Thanks, Ivan
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 21
Jul 2006 14:51:48 remote, seen in Ivan Marsh
if (form.country.options[chooseSelect].value == "FR" ||
form.country.options[chooseSelect].value == "DE" ||
form.country.options[chooseSelect].value == "UK") {

That's grossly inefficient, both in transmission and execution.

var Tmp = form.country.options[chooseSelect].value
if (Tmp == "FR" || Tmp == "DE" || Tmp == "UK") {


If the number of comparisons may vary, the OP can put the test strings
in an array and loop through it; that can conveniently be written as

function IsIn(Val, Arr) { var J
for (J in Arr) if (Val==Arr[J]) return true
return false }

if (IsIn(form.country.options[chooseSelect].value, ["FR", "DE", "UK"])) {
 
D

Douglas Crockford

Roy said:
What's the correct syntax for 'or' for selecting any of a group of countries
from a drop-down list?

This is what I thought should work, but it doesn't seem to.

if (form.country.options[chooseSelect].value == "FR" || "DE" || "UK") {
do this
} else {
do that
}

There are lots of ways to do that, such as a correctly written if, or a switch
statement. Or you could do something more table driven and functional.

var country_action = {
FR: do_this,
DE: do_this,
UK: do_this,
US: do_that,
CA: do_that,
NK: do_other
};

In this case, do_this, do_that, and do_other are the names of functions that
deal with specific countries.

Then, when you pull values from the drop down list, you can do this:

country_action[form.country.options[chooseSelect].value]();

http://javascript.crockford.com/
 
I

Ivan Marsh

JRS: In article <[email protected]>, dated Fri, 21
Jul 2006 14:51:48 remote, seen in Ivan Marsh
if (form.country.options[chooseSelect].value == "FR" ||
form.country.options[chooseSelect].value == "DE" ||
form.country.options[chooseSelect].value == "UK") {

That's grossly inefficient, both in transmission and execution.

Can you point out where there was a request for efficiency?

The point was to answer the question by illustrating what the poster
wasn't seeing in his code.

Sorry... I didn't know I was being graded on the quality of my correct
answers.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 24
Jul 2006 09:06:33 remote, seen in Ivan Marsh
JRS: In article <[email protected]>, dated Fri, 21
Jul 2006 14:51:48 remote, seen in Ivan Marsh
if (form.country.options[chooseSelect].value == "FR" ||
form.country.options[chooseSelect].value == "DE" ||
form.country.options[chooseSelect].value == "UK") {

That's grossly inefficient, both in transmission and execution.

Can you point out where there was a request for efficiency?

One should always be on the lookout for efficiency. Granted, you may
find this difficult, as you come from a nation notoriously profligate
with resources, especially those provided by others.

Your answer provided what the questioner wanted to know, but not what he
ought to know.
The point was to answer the question by illustrating what the poster
wasn't seeing in his code.

Sorry... I didn't know I was being graded on the quality of my correct
answers.

Then you are very naive. Obviously you have not been reading the
newsgroup with reasonable care and attention.

Read the newsgroup FAQ.
 
R

Randy Webb

Dr John Stockton said the following on 7/24/2006 2:55 PM:
JRS: In article <[email protected]>, dated Mon, 24
Jul 2006 09:06:33 remote, seen in Ivan Marsh
JRS: In article <[email protected]>, dated Fri, 21
Jul 2006 14:51:48 remote, seen in Ivan Marsh
<[email protected]> posted :
if (form.country.options[chooseSelect].value == "FR" ||
form.country.options[chooseSelect].value == "DE" ||
form.country.options[chooseSelect].value == "UK") {
That's grossly inefficient, both in transmission and execution.
Can you point out where there was a request for efficiency?

One should always be on the lookout for efficiency. Granted, you may
find this difficult, as you come from a nation notoriously profligate
with resources, especially those provided by others.

Sesquipedalian behavior is a sign of ignorance, not intelligence. But,
based on your past behavior of incorrectly assuming that a persons
locale is an indication of intelligence makes much of what you say on
the matter to be an exercise in floccinaucinihilipilification[1].
Your answer provided what the questioner wanted to know, but not what he
ought to know.

This is a discussion group, not an educational group. If you want to
discuss it, then fine, do so. But do not try to enforce your pedantic
beliefs on others about it.


[1] <URL:http://en.wikipedia.org/wiki/Floccinaucinihilipilification>
 
L

Lasse Reichstein Nielsen

Randy Webb said:
This is a discussion group, not an educational group. If you want to
discuss it, then fine, do so. But do not try to enforce your pedantic
beliefs on others about it.

This is a newsgroup. People post whatever they like as long as it
stays withing the charter of the group. If it helps someone, educates
someone, or makes someone want to discuss, all the more power to us.

Even if this is a discussion group, that still means that it's not a
questions-and-answers group. Just because someone answered a question
in a way that works, it does not mean that that answer cannot be
discussed. And it often is.


Should one follow up on an otherwise correct post in order to suggest
an improved way of doing the same? Sure, as long as it can be done
politely (i.e., rather "here's a suggestion for improvement" than
"your way sucks and mine rocks"). Everybody learns from that, as long
as it really is an improvement. And if it is arguably not one, then
it's an opening for discussion.

I considered suggesting the same improvement in this case, only for
readability and maintainability, not performance. Only lack of time
prevented me from doing it - after all, improving a correct advice is
less important than correcting wrong advice.


Try not to enforce your views of how to use a newsgroup on others :)

/L 'premature optimization is the root of all evil'
 
I

Ivan Marsh

JRS: In article <[email protected]>, dated Mon, 24
Jul 2006 09:06:33 remote, seen in Ivan Marsh
JRS: In article <[email protected]>, dated Fri, 21
Jul 2006 14:51:48 remote, seen in Ivan Marsh
<[email protected]> posted :

if (form.country.options[chooseSelect].value == "FR" ||
form.country.options[chooseSelect].value == "DE" ||
form.country.options[chooseSelect].value == "UK") {

That's grossly inefficient, both in transmission and execution.

Can you point out where there was a request for efficiency?

One should always be on the lookout for efficiency. Granted, you may
find this difficult, as you come from a nation notoriously profligate
with resources, especially those provided by others.

Your answer provided what the questioner wanted to know, but not what he
ought to know.
The point was to answer the question by illustrating what the poster
wasn't seeing in his code.

Sorry... I didn't know I was being graded on the quality of my correct
answers.

Then you are very naive. Obviously you have not been reading the
newsgroup with reasonable care and attention.

Read the newsgroup FAQ.

You are, as always, an arrogant prick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top