JavaScript - Arrays And If Statment
How do you format a if statment for a multidemensional array.
Similar TutorialsI need to loop the alphabet and numbers 0-9 to initialize a few thousand arrays. This is for my site and is truly needed. http://www.thefreemenu.com I currently have every array written out and it takes up to much space in my .js file. The majority of my variables are empty but necessary and need to be there (including empty) for my site to work properly. Question is the last part Here's where I'm at. Code: var NewVarLetterOrNum = "a"; eval("_oneofseveralnames_" + NewVarLetterOrNum + "='this part works';"); alert(_oneofseveralnames_a); This creates the variable _oneofseveralnames_a='this part works' Code: var newArrayLetterOrNum = "a"; eval("_oneofseveralnames_" + newArrayLetterOrNum + "= new Array();"); alert(_oneofseveralnames_a) This creates the Array _oneofseveralnames_a=new Array(); and all the values in the array are null, but, now a variable like _nl_a[1]='something' can be used elsewhere because the array exists. This is all that is necessary for now because I can probably set all the variables to be blank with something like Code: i=1 while(i<=20){ _oneofseveralnames_a[i]="1-20"; i++ } alert(_oneofseveralnames_[20]); So now you have what I came to understand in the first few hours. Now to the hard part : ( I can't make multiple array's dynamically. I dont' know if its because I don't understand loops or arrays or what and its very fustrating. As for any answer you might be so kind as to provide, if you could dumb it down that would be greatly appreciated. Code: var newArray =new Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z') i=1 while(i<=26){ eval("_nl_" + newArray[i] + "= new Array();"); i++ } alert(newArray[1]) // Is b, but alert(_nl_b) //I can't get _nl_b to exist, I tried everything including taking away the quotes around the letters in every test */ var _nl_a =new Array() var _img_a =new Array() var _h_a =new Array() var _r_a =new Array() var _m_a =new Array() var _yt_a =new Array() var _i_a =new Array() The above arrays are all the array _name_ parts I need but for example, a has 10 parts, a,p2_a,p3_a,.. p10_a. I need 10 pages for each letter of the alphabet and numbers 0-9 and a special all1, p2_all1 ... p10_all1. Overall 2200 arrays that need to be declared. Currently they are all written out. /* I want the output to be this way Student: Doe, John eMail: jd@email.com Course ID: COIN-O70A.01 ----------------------- Can any one pls help me with the display. If I use alert instead of return then I works. But I need to display it in the document. Code: <html> <head> <title>Variable - Examples 1</title> <script type="text/javascript"> function Student(firstName, lastName, email,courseID){ this.firstName = firstName; this.lastName = lastName; this.email = email; this.courseID = courseID; } Student.prototype = { constructor : Student, toString : studentInfo }; function studentInfo(){ return this.firstName + "," + this.lastName + '\n'+ this.email + '\n' + this.courseID; } var student = new Student("Doe", "John", "Doeohn@gmail.com","COIN 70A.01"); </script> </head> <body> <script type="text/javascript"> document.writeln(student.toString()); </script> </body> </html> Code: var areas = { "The Plains of CASCADIAN": { yMin: 0, yMax: 20, xMin: 0, xMax: 24 }, "The Forests of BELGROS": { yMin: 0, yMax: 34, xMin: 24, xMax:50 }, "Bewitched VOLRAP": { yMin: 0, yMax: 42, xMin: 50, xMax:100 }, "The Moutains of MELDROSS": { yMin: 26, yMax: 52, xMin: 0, xMax: 48 }, "The Moutains of OLAP": { yMin: 49, yMax: 68, xMin: 0, xMax: 48 }, "The Swamps of NEMBRESS": { yMin: 54, yMax: 90, xMin: 44, xMax: 100 }, "The Great Wood of OLKLEMPT": { yMin: 62, yMax: 100, xMin: 32, xMax: 100 }, "Dark Forests of MURLAP": { yMin: 60, yMax: 100, xMin: 0, xMax: 42 }, "Imperial MANTRIOCK": { yMin: 22, yMax: 58, xMin: 44, xMax:100 } } How would you reffer to the areas with a javascript if statment or case? This code is not working correctly when you type get_data it should only work if you already have typed load_hacker. But get_data works withought load_hacker.
Code: <script type="text/javascript"> function command(){ if(command.value = "load_hacker" && level.value == 1){ document.getElementById("command").value = ""; document.getElementById("number").value = ""; plus(); } else { document.getElementById("command").value = "..."; document.getElementById("number").value = "..."; } if(command.value = "get_data" && level.value == 2){ document.getElementById("command").value = ""; document.getElementById("number").value = ""; plus(); } else { document.getElementById("number").value = "..."; document.getElementById("command").value = "..."; } } </script> <script type="text/javascript"> function plus(){ document.getElementById("level").value ++; } </script> ><input type="text" value="" id="command" size="" style="background-color:transparent;border:0px solid white;" /> #<input type="text" value="" id="number" size="10" style="background-color:transparent;border:0px solid white;" /> <input type="button" value="Enter" onclick="command()" /> <input type="hidden" value= 1 id="level" /> was wondering if it made sense to optimize the comparison order inside an if() statement if one of the comparison targets is a function call eg: Code: var a = true, b = function() {return false;}; if (b() || a) {...} if (a || b()) {...} would the second statement run faster because it would theoretically never need to call b() in this situation? can the eval order be relied on? does this depend on the js engine/internal optimizations? thanks, Leon Could you make a if statment for adding points to Fel,Dom or Blob. <script type="text/javascript"> Blob = 500 Fel = 300 Dom = 400 if (Fel == Dom && Dom == Blob) { document.write("<b>It was a tie!</b>") } else if (Blob == Dom || Dom == Blob) { document.write("<b>Dom and Blob!</b>"); } else if (Fel == Dom || Dom == Fel) { document.write("<b>Dom and Fel!</b>"); } else if (Blob == Fel || Fel == Blob) { document.write("<b>Blob and Fel!</b>"); } if (Fel > Blob || Fel > Dom ) { document.write("<b>Fel!</b>"); } else if (Dom > Fel || Dom > Blob) { document.write("<b>Dom!</b>"); } else if (Blob > Dom || Blob > Fel) { document.write("<b>Blob!</b>"); } </script> First of all thank you for reading my thread and giving time to help me. Here goes my question: How to sort this array? MY PRIORITY COLORS array: ------------ green , yellow ALL COLORS array : <---- this will be your main array. ----------------------- pink , green , orange THE RESULTS: ( the results after you sort) (Not sorted by alphabetically, but sorted by the priority colors) ---------------- green , pink , orange I hope you could help me solve this javascript sorting problem. Thank you! Hi could someone help with this javascript. i dont know where to start with the script which is in bold. Program to report the results of an election. // candidates var candidateArray = ['Mr R Green...', 'Ms O Brown...', 'Ms Y Black...', 'Mr G White...', 'Ms B Grey....','Ms I Blue....', 'Mr V Pink....']; // online votes var onlineVotesArray = [21,47,23,11,56,47,30]; // paper votes var paperVotesArray = [12,4,20,11,5,4,17]; // total votes -- to be initialised below var totalVotesArray; // Add code to // -- initialise totalVotesArray with a new empty array of the same size as candidateArray // -- use a for loop to add the online and paper votes for each candidate and store the result at the corresponding position in the total votes array. Hi. I am fairly new to JavaScript. I am having some troubles with arrays. I am using Chrome 12 beta. Here is a brief version of the code I am having trouble with Code: var array=new Array() array[0]=new Array() array[0][1]=new Array() var array[0][1]=[ 3, 1, 4, 1, 5, 9, 2, 6, 5, ... ]; I do not want to use "array[0][1][n]" because my array has too many items to do that and it is just plain annoying to change the numbers that way. I get an error on the "var buffers[0][1]=[" line saying: "Uncaught SyntaxError: Unexpected token [" What is the problem in my code? Thank you ahead of time. ---mint Code: <HTML> <HEAD> <TITLE>Listing 4.4</TITLE> <SCRIPT TYPE="text/javascript"> //DEFINE METHOD function displayInfo() { document.write("<H1>Employee Profile: " + this.data[0] + "</H1><HR /><PRE>"); document.writeln("Employee Number: " + this.data[1]); document.writeln("Social Security Number: " + this.data[2]); document.writeln("Annual Salary: " + this.data[3]); document.write("</PRE>"); } //DEFINE METHOD TO GET EMPLOYEE INFORMATION function getInfo() { var menu="0-Exit/1-Name/2-Emp. #/3-Soc. Sec. #/4-Salary"; var choice=prompt(menu,"0"); if (choice != null) { if ((choice < 0) || (choice > 4)) { alert ("Invalid choice"); this.getInfo(); } else { if (choice != "0") { this.data[choice-1]=prompt("Enter information",""); this.getInfo(); } } } } //DEFINE OBJECT function employee() { this.data = new Array(4); this.displayInfo=displayInfo; this.getInfo=getInfo; } newEmployee=new employee(); </SCRIPT> </HEAD> <BODY> <SCRIPT TYPE="text/javascript"> newEmployee.getInfo(); newEmployee.displayInfo(); </SCRIPT> </BODY> </HTML> I am currently taking a Javascript certification course, this is a exercise given (final code) I don't completely understand what is going on though, could someone explain to me Code: this.data[choice-1]=prompt("Enter information",""); that part? I don't understand why, [choice-1] I don't fully understand why an array is even needed here? This has been resolved. The moment i posted it i realised the mistake i was making . Learnig how to delete my post, until then thought i would sdimply delete the content.
Hi again folks! A code I did recently was that 10 football teams had to register and the program had to ask the question "please enter your team" ten times then show the display on screen. This code works fine and does it jobs but I realized that I did this program a long winded way (shown below) : <script type="text/javascript"> //ask to enter names var enteredteam1= prompt("Please enter first team.", ""); //each team is shown as different variable var enteredteam2= prompt("Please enter second team.", ""); var enteredteam3= prompt("Please enter third team.", ""); var enteredteam4= prompt("Please enter forth team.", ""); var enteredteam5= prompt("Please enter fifth team.", ""); var enteredteam6= prompt("Please enter sixth team.", ""); var enteredteam7= prompt("Please enter seventh team.", ""); var enteredteam8= prompt("Please enter eighth team.", ""); var enteredteam9= prompt("Please enter ninth team.", ""); var enteredteam10= prompt("Please enter final team.", ""); var team= []; team[1]= enteredteam1 //the team should equal the entered value for the space team[2]= enteredteam2 team[3]= enteredteam3 team[4]= enteredteam4 team[5]= enteredteam5 team[6]= enteredteam6 team[7]= enteredteam7 team[8]= enteredteam8 team[9]= enteredteam9 team[10]= enteredteam10 document.write("<h1>Registered Teams</h1>"); // display the teams on the screen beside number document.write("<b>1.</b>" + team[1]); //write the entered value on the screen document.write("<br><br><b>2.</b>" + team[2]); document.write("<br><br><b>3.</b>" + team[3]); document.write("<br><br><b>4.</b>" + team[4]); document.write("<br><br><b>5.</b>" + team[5]); document.write("<br><br><b>6.</b>" + team[6]); document.write("<br><br><b>7.</b>" + team[7]); document.write("<br><br><b>8.</b>" + team[8]); document.write("<br><br><b>9.</b>" + team[9]); document.write("<br><br><b>10.</b>" + team[10]); </script> I was wondering for future references could anyone suggest a simpler code to do something similar? For instance what if I wanted to ask the question but I didn't know how many teams were going to be entered. It would ask the question (enter your team) until the user clicked on a button saying "finished" or something. Once again, I am new to this field, so any code with comments would be helpful! is it possible to embed something like this Code: <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='450' height='24'> <param name='movie' value='http://youtubemp3.tv/player/player.swf'> <param name='allowfullscreen' value='false'> <param name='allowscriptaccess' value='always'> <param name='wmode' value='transparent'> <param name='flashvars' value='file=http://youtubemp3.tv/mp3embed-oeduvhv516z9.mp3&duration='> <embed src='http://youtubemp3.tv/player/player.swf' width='450' height='24' allowscriptaccess='always' allowfullscreen='false' flashvars='file=http://youtubemp3.tv/mp3embed-oeduvhv516z9.mp3&duration=' /> </object> into a array that will randomly pull things from it like this Code: <input type = "button" value="Call Out a Target" onclick="myFunction()"></button> <div id = "demo1"></div> <div id = "demo2"></div> <script type="text/javascript"> function shuffle(array) { var n = array.length, k, t; if (n == 0) {return false}; while (--n) { k = Math.floor(Math.random() * (n+1)); t = array[n]; array[n] = array[k]; array[k] = t; } } function myFunction() { var myarray=["red","blue", "yellow", "orange"]; shuffle (myarray); document.getElementById("demo2").innerHTML = myarray[0]; } </script> Thanks!!! I have a quick question with multiple array and random numbers. If i generate my random numbers in one array, how would i take a selection of those numbers and put them in another array? Ex: array 1: 25, 34, 38, 40, 22, 49 want to move numbers between 30 and 50 to another array. array 2: 34, 38, 40, 49 is it as simple as for loops and if statements setting the conditions? do i use a sorting method? (selection? bubble?) any help would be appreciated. Code: <script type="text/javascript"> var authors = ['Ernst Hemingway','Charlotte Bronte','Dante Alighieri','Emily Dickinson']; </script> <script type="text/javascript"> document.write('<p>The first author is <strong>'); document.write('authors[0] + '</strong> </p>'); </script> I'm getting a syntax error on the last line ? I can't see the error, I closed the strong tag, closed the p tag. Put the line in a string, why won't it write the string ? Hi, I am trying to merge the values of 2 arrays. array 1 = 1, 2, 3, 4, 5 array 2 = a, b, c, d, e Resulting in: array 3 = 1-a, 2-b, 3-c, 4-d, 5-e etc both arrays that will be used originally will always of the same length. Both arrays will contain numbers Can you please help? Thanks Do arrays retain its data outside of a function. The reason why i asked this is because i created a global array and then a function. In the function i assaigned values to the global array but when i try to access the global array it gives me a "undefined" or "null" error. Code: var myArray = new Array(); var count = 0; function populate(){ for(i=0; i < 5; i++){ myArray[i] = count++; } } Im working on a program where the user enters a line of numbers and then can either sort them in order or can display the minimum number. Both those seem to work, but for some reason I keep getting three -1 values in my array. So the minimum value is always -1 and when I sort it comes back -1-1-1[numbers i enter]. I cant seem to figure out why my code is doing that. Code: private int[] buildArray(String line) { int[]arr = new int[line.length()]; arr[0] = 0; for (int i = 1; i < line.length(); i++){ arr[i] = Character.digit(line.charAt(i),10); } return arr; } private int findMin(int numbers[]) { //PRE numbers.length >= 1 int min = numbers [0]; for (int i = 1; i < numbers.length; i++){ if (numbers [i] < min) { min = numbers [i]; } } return min; } private String makeString(int numbers[]) { //PRE numbers.length >= 1 int i = 0; String s1 = new String (""); while (i < numbers.length){ s1 = numbers[i] + s1; i++; } return s1; } private void sort(int numbers[]) { for (int i = 1; i< numbers.length; i++){ int j = i-1; int temp = numbers[i]; while (j >= 0 && temp > (numbers[j])){ numbers[j+1] = numbers[j]; j--; } numbers[j+1] = temp; } } Input: 3,1,2,88 output: Min Value = -1 Line 1 Sorted = -1-1-101288 Any type of help would be very appreciated, Im very new to this still. |