Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding out if a number is in an Array

Status
Not open for further replies.

NateUNI

MIS
Jan 3, 2002
132
US
I want to do the following:
If i have the number, say 55, i want to construct a IF statment the checks if the number 55 is in an array of numbers, ie.

IF 55 in ArrayOfNumbers{
Statements...
Else
Statements...

Thanks for the help,
Newbie
 
hey nate, try something like this
Code:
for (O=0;O<ArrayOfNumbers.length;O++)
{
if (ArrayOfNumbers[O] == 55)
     {
     //do whatever here;
     }
else {
     // do whatever if it isnt found
     }
}
hope it helps
&quot;Those who dare to fail miserably can achieve greatly.&quot; - Robert F. Kennedy
So true..
 
Try this:

Code:
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--

    var myArr = new Array (1, 2, 3, 55, 4, 5); // Array

    function findArrItem (arr, match) { // Array finding function
        for (i = 0; i < arr.length; i++) {
            if (arr[i] == match) {
                return true;
            }
        }
        return false;
    }

    if (findArrItem (myArr, 55)) {  // Statement to find array item
        alert (&quot;Item is found!!&quot;);
    } else {
        alert (&quot;Item isn't found!!&quot;);
    }

//-->
</script>
Regards
David Byng
bd_logo.gif

davidbyng@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top