Jump to content

JavaScript and Array of Strings


Recommended Posts

Hi all, I have a project which the main idea is: to recieve a string variable from the first page and pass it to the second page, Then I should make it run inside a loop for an array of strings, and if it match any part of that (element) string then it should print it, This is what I have till now for example:

var search = "news"var myarray = new Array(3)myarray[0] = "news world"myarray[1] = "computer"myarray[2] = "my news world"for (var x = 0, var max = myarray.length; x < max; x++){if myarray[x].match(search){document.write(myarray[x] + "<br />");}}

So here it should print myarray[0] and myarray[2] strings? if there is any error can u tell me about it please..Also I want to know if I can do something like (REQUEST.QUERYSTRING) to request the variable passed from the first page to the second page.Thanks in advance,zeid

Link to comment
Share on other sites

This works:

<script type="text/javascript"><!--var search = "news";var myarray = new Array(3);myarray[0] = "news world";myarray[1] = "computer";myarray[2] = "my news world";for(var x = 0; x < myarray.length; x++) {   if(myarray[x].match(search)) {   document.write(myarray[x] + "<br />");   }}// --></script>

1. You don't need to inlude the var max = myarray.length, and you had a "," in your for statement.2. You were missing a "(" and ")" in the if statement.:) hope that helps

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...