Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    215
    Thanks
    14
    Thanked 0 Times in 0 Posts

    What's wrong with this syntax?

    Hello all. I am trying to compare a string with an array and want to know if there is a match. If there is, I want an alert, else just continue.

    Here is what I have:

    Code:
    <Script>
       var msg = "9/80 Did Not Work";
       for (var i in Case2)
       {
          if(msg == Case2[i]){
          alert ("There is a match!" + Case2[i])
          }
       }
       
    </Script>
    Although, if I change if(msg != Case2[i]){ then I get ALL of them as a result, including "9/80 Did Not Work".

    Thanks for your help,

    Parallon

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    What are the contents of the Case2 array?

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    215
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Here are the contents along with the corresponding index number:

    Code:
    Case2[0]All Projects for Week Project Manager
    Case2[1]106387 1.04.01 ES&H Support
    Case2[2]106387 1.04.01.13 Audits Walk Throughs
    Case2[3]106387 1.04.01.14 Gen Facility ES&H
    Case2[4]106387 1.04.01.15 Safety Equipment
    Case2[5]106387 1.04.01.16 Behavior Based Safety
    Case2[6]106387 1.04.01.17 Electrical Safety
    Case2[7]106387 1.04.01.18 Misc Compliance Requirements
    Case2[8]106387 1.04.02 Organization Training
    Case2[9]106387 1.02.05 Centrifuge
    Case2[10]106387 1.02.03 Photometrics
    Case2[11]106387 1.02.06 Shock
    Case2[12]106387 1.02.11 MIU
    Case2[13]106387 1.02.08 Burnsite
    Case2[14]106387 1.02.09 Thermal Test Complex
    Case2[15]106387 1.02.01 Sled Track
    Case2[16]106387 1.02.02 Cable Site
    Case2[17]106387 1.02.04 Impact / Towers
    Case2[18]106387 1.02.12 Blast Tubes
    Case2[19]106387 1.02.14 Central Services
    Case2[20]106387 1.02.10 Laser Tracker
    Case2[21]9/80 Did Not Work
    Thanks,

    Parallon

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Check if there are any leading and trailing spaces by enclosing them in quotes.
    Code:
    for (var i in Case2)
       {
          alert("'" + msg + "'\n'" + Case2[i] + "'");
          if(msg == Case2[i]){
             alert ("There is a match!" + Case2[i])
          }
       }

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Put the values in your array in quotes
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #6
    Regular Coder
    Join Date
    May 2005
    Posts
    215
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thank you both. That really helped me see the differences in my strings. All is working fine and dandy now!

    Parallon


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •