How to get Javascript array length?

Javascript selecting an array?

  • My original question was. I am creating a game that uses a function that checks whether an integer exists within an array. If it does, then the function will perform A otherwise it performs B. For example this is what I have. if(isitthere(image)){ //image is an integer. So image may ==1 I have several arrays (like this). array0= new Array(1,2); array1= new Array(3,4); array2= new Array(5,6); I am using a random number generator to select a number between 0-3. This is the function I am using to check whether image (integer) exists within the array. function isitthere(obj){ var num=Math.floor(Math.random()*3) var j=false; for(var hits=0;hits<array.length;hits++){ if(array[hits]==obj){ j=true; break; } } return j; } However, notice that I haven't stated the number after array in the FOR or IF loop. In other words, I have only said array and not array1 or array2 or array3. This is because I want to concatenate the word array with the random number created. So in effect, the function selects 1 of the possible 3 arrays. However this does not work! If I literally write array0 or array1 or array2, it works fine. But as soon as I write array+num it does not work and it always performs the else and skips the IF. Sorry if i havent explained myself well enough :/ but any solutions on how to add the random number to the word array so that the function selects a random array would be great!!! thank you ______________________________________… One user answered with. You don't want to do that. Trust me. You want to create another array and put your three arrays in it. So it would work like this: var array0 = [1, 2]; // never use the "new Array" syntax var array1 = [3, 4]; var array2 = [5, 6]; var bigArray = [array0, array1, array2]; Then, in your function, you can do something like: var num = Math.floor(Math.random()*3); var arrayToUse = bigArray[num]; And then use arrayToUse in your loop. ______________________________________… My next question is. Basically what i'm creating is battleships. I want to store a variety of positions in different arrays - so the player can play the game again and again without having the same positions. Because at the moment I have this code. play=true; shipstate=0; unhidden=0; state = new Array(); for(i=1;i 3 minutes ago Basically what i'm creating is battleships. I want to store a variety of positions in different arrays - so the player can play the game again and again without having the same positions. Because at the moment I have this code. play=true; shipstate=0; unhidden=0; state = new Array(); for(i=1;i 53 seconds ago state = new Array(); for(i=1;i<101;i++){state[i]=0;} function select(image){ if(play && !state[image]){ unhidden++; if(image==1 || image==2)){ document["img"+image].src="red.png"; state[image]=1; } else{ document["img"+image].src="white.png"; state[image]=2; } } So currently, if the player was to refresh the page then he or she would already know that by clicking the first and second image would result in them hitting a ship. Hence this is why I don't want to use 'image==1 || image==2' but rather refer to an array that is randomly selected. So if I were to use var array0=[1,2]; var array1=[3,4]; var array2=[5,6]; var bigArray=[array0, array1, array2]; And also use: var num=Math.floor(Math.random()*3); var arrayToUse=bigArray[num]; How would I add that into the code I've already got so I can remove 'image==1 || image==2'. So that i've bombarded you with a question. I am already grateful for your advice and would love some more! please!

  • Answer:

    Define a two dimention array. Array is a reserved word so use something more imaginative for the name. var myNumbers=new Array(); numbers[0] = [1,2]; numbers[1] = [3,4]; numbers[2] = [5,6]; to access numbers[i][j] Have fun.

James at Yahoo! Answers Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.