...

View Full Version : What's wrong with this syntax?



parallon
06-15-2007, 09:10 PM
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:


<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

glenngv
06-15-2007, 10:38 PM
What are the contents of the Case2 array?

parallon
06-15-2007, 10:43 PM
Here are the contents along with the corresponding index number:


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

glenngv
06-16-2007, 12:17 AM
Check if there are any leading and trailing spaces by enclosing them in quotes.

for (var i in Case2)
{
alert("'" + msg + "'\n'" + Case2[i] + "'");
if(msg == Case2[i]){
alert ("There is a match!" + Case2[i])
}
}

Mr J
06-16-2007, 07:48 AM
Put the values in your array in quotes

parallon
06-18-2007, 07:11 PM
Thank you both. That really helped me see the differences in my strings. All is working fine and dandy now! :)

Parallon



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum