Forum Moderators: open
<html>
<head>search for data in an array
<title>test </title>
<script language="JavaScript" >
var test1 = ['12345#ABC/1','123ACDE#BCC/2'];
var test2 = '';
test2 = test1[0];
for (counter = 0; counter <= test1.length; counter = counter + 1)
{
};
<BR>
document.write('The number in the array is ' + test2 + 'and so is true')
</script>
</head>
<body>
</body>
</html>
var x = "abcd-efghi";
var dashLocation = x.indexOf("-"); // 4
var threeChars = x.substr( dashLocation + 1, 3 );
Thus, applying this to your code (but removing the NUMEROUS errors in your code first)...
<html>
<head>search for data in an array</head>
<title>test</title>
<script type="text/javascript">
var test1 = ['12345#ABC/1','123ACDE#BCC/2'];
for (var counter = 0; counter <= test1.length; counter++ )
{
var dashLocation = test1[counter].indexOf("#");
var threeChars = test1[counter].substr( dashLocation + 1, 3 );
if( threeChars == "ABC" )
{
alert("Yup... found ABC");
// Stop looping
break;
}
}
</script>
</head>
<body>
</body>
</html>
var threeChars = test1[counter].substr( dashLocation + 1, 3 );
var reg = /[Aa][.][Cc]/;
if( reg.test(threeChars) )
{
alert("Yup... found A*C");
// Stop looping
break;
}
I *think* this will match any string that begins with an 'A' or 'a', followed by any character, followed by a 'C' or 'c'.
<html>
<head>search for data in an array</head>
<title>test</title>
<script type="text/javascript">
var test1 = ['12345#ABC/1','123ACDE#BCC/2'];
for (var counter = 0; counter <= test1.length; counter++ )
{
var dashLocation = test1[counter].indexOf("#");
var threeChars = test1[counter].substr( dashLocation + 1, 3 );
if( threeChars == "ABC" )
{
alert("Yup... found ABC");
// Stop looping
break;
}
}
</script>
</head>
<body>
</body>
</html>
<html>
<head>search for data in an array</head>
<title>test</title>
<script type="text/javascript">
var test1 = ['12345#ABC/1','123ACDE#BCC/2'];
for (var counter = 0; counter < test1.length; counter++ )
{
var dashLocation = test1[counter].indexOf("#");
var threeChars = test1[counter].substr( dashLocation + 1, 3 );
var reg = /[Aa].[Cc]/;
if( reg.test(threeChars) )
{
alert("Yup... found A*C");
// Stop looping
break;
}
}
</script>
</head>
<body>
</body>
</html>
Wow, thanks for that. How long have you been programming in Javascript? I feel i have a long way to go still!
I've been using JavaScript for about 10 years, but I didn't REALLY start using it until the past year or so.
If i wanted to change the code so it does all it did before but also if the array contains the letter W to return a different result again?
Can you be more specific? Can you give an example of what the array might look like (what values would be in it)? Would you want it to NOT check for A.C if it found the W?
<html>
<head>search for data in an array</head>
<title>test</title>
<script type="text/javascript">
var test1 = ['12345#ABC/1','123ACDE#BCC/2'];
var foundW = false;
var foundAxC = false;
for (var counter = 0; counter < test1.length; counter++ )
{
if( test1[counter].indexOf("W") >= 0 )
{
foundW = true;
break;
}
var dashLocation = test1[counter].indexOf("#");
var threeChars = test1[counter].substr( dashLocation + 1, 3 );
var reg = /[Aa].[Cc]/;
if( reg.test(threeChars) )
{
foundAxC = true;
// Stop looping
continue;
}
if( foundW )
{
alert("Found W");
}
else if( foundAxC )
{
alert("Found AxC");
}
else
{
alert("Didn't find W or AxC");
}
}
</script>
</head>
<body>
</body>
</html>
Note, if it finds a W, it will break out of the for loop. But if it finds an AxC, it will continue to the next item in the loop. It does that in case it finds an AxC before finding a W that appears later in the array.
[edited by: Fotiman at 10:54 pm (utc) on Feb. 5, 2007]
I see it is declared and initialised with a value of false, and there is an if statement but whereabouts is the code that is looking for W? i can follow the logic for the ABC statement, but not quite following the search and stop for the W.
<html>
<head>
<title>test</title>
<script type="text/javascript">
var test1 = ['12345#ABC/1','123ACDE#BCC/W'];
var foundW = false;
var foundAxC = false;
for (var counter = 0; counter < test1.length; counter++ )
{
if( test1[counter].indexOf("W") >= 0 )
{
foundW = true;
break;
}
var dashLocation = test1[counter].indexOf("#");
var threeChars = test1[counter].substr( dashLocation + 1, 3 );
var reg = /[Aa].[Cc]/;
if( reg.test(threeChars) )
{
foundAxC = true;
// Stop looping
continue;
}
}
if( foundW )
{
alert("Found W");
}
else if( foundAxC )
{
alert("Found AxC");
}
else
{
alert("Didn't find W or AxC");
}
</script>
</head>
<body>
</body>
</html>
[edited by: Fotiman at 9:20 pm (utc) on Feb. 6, 2007]
<html>
<head>
<title>test</title>
<script type="text/javascript">
var test1 = ['12345#ABC/1','123ACDE#BCC/2','123ACDE#ABC/W'];
var results = new Array();
for (var counter = 0; counter < test1.length; counter++ )
{
if( test1[counter].indexOf("W") >= 0 )
{
results[results.length] = "Element " + (counter + 1) + " is [" + test1[counter] + "] and contains 'W'";
continue;
}
var dashLocation = test1[counter].indexOf("#");
var threeChars = test1[counter].substr( dashLocation + 1, 3 );
var reg = /[Aa].[Cc]/;
if( reg.test(threeChars) )
{
results[results.length] = "Element " + (counter + 1) + " is [" + test1[counter] + "] and contains '" + threeChars + "'";
continue;
}
results[results.length] = "";
}
// Here are the results:
var resultsStr = "";
for( var i = 0; i < results.length; i++ )
{
resultsStr += (results[i] + (results[i].length > 0? "\n" : ""));
}
if( resultsStr.length > 0 )
alert(resultsStr);
</script>
</head>
<body>
</body>
</html>
[edited by: Fotiman at 9:32 pm (utc) on Feb. 6, 2007]