Strings and OR

Discussion in 'Javascript' started by Roy Reed, Jul 21, 2006.

  1. Roy Reed

    Roy Reed Guest

    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
     
    Roy Reed, Jul 21, 2006
    #1
    1. Advertising

  2. Roy Reed

    Ivan Marsh Guest

    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.
     
    Ivan Marsh, Jul 21, 2006
    #2
    1. Advertising

  3. Roy Reed

    Roy Reed Guest

    "Ivan Marsh" <> wrote in message
    news:p...
    > 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
     
    Roy Reed, Jul 21, 2006
    #3
  4. JRS: In article <>, dated Fri, 21
    Jul 2006 14:51:48 remote, seen in news:comp.lang.javascript, Ivan Marsh
    <> 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.
     
    Dr John Stockton, Jul 22, 2006
    #4
  5. 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/
     
    Douglas Crockford, Jul 22, 2006
    #5
  6. Roy Reed

    Ivan Marsh Guest

    On Sat, 22 Jul 2006 17:55:42 +0100, Dr John Stockton wrote:

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

    Randy Webb Guest

    Dr John Stockton said the following on 7/24/2006 2:55 PM:
    > JRS: In article <>, dated Mon, 24
    > Jul 2006 09:06:33 remote, seen in news:comp.lang.javascript, Ivan Marsh
    > <> posted :
    >> On Sat, 22 Jul 2006 17:55:42 +0100, Dr John Stockton wrote:
    >>
    >>> JRS: In article <>, dated Fri, 21
    >>> Jul 2006 14:51:48 remote, seen in news:comp.lang.javascript, Ivan Marsh
    >>> <> 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/
     
    Randy Webb, Jul 25, 2006
    #8
  9. Randy Webb <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jul 25, 2006
    #9
  10. Roy Reed

    Ivan Marsh Guest

    On Mon, 24 Jul 2006 19:55:18 +0100, Dr John Stockton wrote:

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

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Krueckeberg
    Replies:
    2
    Views:
    716
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    386
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    498
    Klaus Neuner
    Jul 26, 2004
  4. Girish Sahani
    Replies:
    17
    Views:
    573
    Boris Borcic
    Jun 9, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
Loading...

Share This Page