Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Strings and OR

Reply
Thread Tools

Strings and OR

 
 
Roy Reed
Guest
Posts: n/a
 
      07-21-2006
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


 
Reply With Quote
 
 
 
 
Ivan Marsh
Guest
Posts: n/a
 
      07-21-2006
On Fri, 21 Jul 2006 20:42:55 +0100, Roy Reed wrote:

> 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") {

--
The USA Patriot Act is the most unpatriotic act in American history.
Feingold-Obama '08 - Because the Constitution isn't history,
It's the law.

 
Reply With Quote
 
 
 
 
Roy Reed
Guest
Posts: n/a
 
      07-21-2006

"Ivan Marsh" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> On Fri, 21 Jul 2006 20:42:55 +0100, Roy Reed wrote:
>
>> 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


 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      07-22-2006
JRS: In article <(E-Mail Removed)>, dated Fri, 21
Jul 2006 14:51:48 remote, seen in news:comp.lang.javascript, Ivan Marsh
<(E-Mail Removed)> 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.

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"])) {

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Douglas Crockford
Guest
Posts: n/a
 
      07-22-2006
Roy Reed wrote:
> 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/
 
Reply With Quote
 
Ivan Marsh
Guest
Posts: n/a
 
      07-24-2006
On Sat, 22 Jul 2006 17:55:42 +0100, Dr John Stockton wrote:

> JRS: In article <(E-Mail Removed)>, dated Fri, 21
> Jul 2006 14:51:48 remote, seen in news:comp.lang.javascript, Ivan Marsh
> <(E-Mail Removed)> 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?

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.

--
The USA Patriot Act is the most unpatriotic act in American history.
Feingold-Obama '08 - Because the Constitution isn't history,
It's the law.

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      07-24-2006
JRS: In article <(E-Mail Removed)>, dated Mon, 24
Jul 2006 09:06:33 remote, seen in news:comp.lang.javascript, Ivan Marsh
<(E-Mail Removed)> posted :
>On Sat, 22 Jul 2006 17:55:42 +0100, Dr John Stockton wrote:
>
>> JRS: In article <(E-Mail Removed)>, dated Fri, 21
>> Jul 2006 14:51:48 remote, seen in news:comp.lang.javascript, Ivan Marsh
>> <(E-Mail Removed)> 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.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      07-25-2006
Dr John Stockton said the following on 7/24/2006 2:55 PM:
> JRS: In article <(E-Mail Removed)>, dated Mon, 24
> Jul 2006 09:06:33 remote, seen in news:comp.lang.javascript, Ivan Marsh
> <(E-Mail Removed)> posted :
>> On Sat, 22 Jul 2006 17:55:42 +0100, Dr John Stockton wrote:
>>
>>> JRS: In article <(E-Mail Removed)>, dated Fri, 21
>>> Jul 2006 14:51:48 remote, seen in news:comp.lang.javascript, Ivan Marsh
>>> <(E-Mail Removed)> 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>
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      07-25-2006
Randy Webb <(E-Mail Removed)> writes:

> 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'
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Ivan Marsh
Guest
Posts: n/a
 
      07-26-2006
On Mon, 24 Jul 2006 19:55:18 +0100, Dr John Stockton wrote:

> JRS: In article <(E-Mail Removed)>, dated Mon, 24
> Jul 2006 09:06:33 remote, seen in news:comp.lang.javascript, Ivan Marsh
> <(E-Mail Removed)> posted :
>>On Sat, 22 Jul 2006 17:55:42 +0100, Dr John Stockton wrote:
>>
>>> JRS: In article <(E-Mail Removed)>, dated Fri, 21
>>> Jul 2006 14:51:48 remote, seen in news:comp.lang.javascript, Ivan Marsh
>>> <(E-Mail Removed)> 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.

--
The USA Patriot Act is the most unpatriotic act in American history.
Feingold-Obama '08 - Because the Constitution isn't history,
It's the law.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments