JavaScript - Setting Up And Looping Through A 3d Array?
Hi,
I'm banging my head off a brick wall with setting up a 3 dimensional array and trying to loop through it. I'm using the EJS framework (http://embeddedjs.com/). We currently have a 2D array set up to list out features. See below: topfeatures: [ "Feature 1", "Feature 2", "Feature 3", "Feature 4", "Feature 5", "Feature 6" ] <ul> [% for(var i = 0; i < this.topfeatures.length; i++) { %] <li>[%= this.topfeatures[i] %]</li> [% } %] </ul> However, the request we have requires headlines for each set of features. Headline 1 Feature1 Feature2 Feature3 Headline 2 Feature4 Feature5 Feature6 Any ideas how I can do this? Similar TutorialsHi, I'm a JS beginner and I find looping through arrays with for/in is very easy. Yet I find lots of code examples where array length is used instead of for/in and I'm thinking to myself, why do it this (somewhat) hard(er) way? Maybe I'm missing something... Thanks for your help. Code: <SCRIPT LANGUAGE = "JAVASCRIPT"> var contestantNamesArray = ['Tom and Nazia', 'Pat and Dan', 'Sandra and Kofi', 'Ian and Adele', 'Paul and Costas']; var judgesPointsArray = [2,1,5,4,3]; var audiencePointsArray = [4,5,2,3,1]; //code to declare a new array to store the combined points for each couple var combinedPointsArray=new Array(); //code to calculate the combined points for each couple and store it in the combined points array for (i = 0; i <= 4; i++) { combinedPointsArray[i] = judgesPointsArray[i] + audiencePointsArray[i]; //code to find and output the maximum value in the combined points array } var winner = [0]; //set first pair as default winner for (i = 1; i <= 4; i++) { //start with second pair if (combinedPointsArray[i] => combinedPointsArray[winner[0]]) { //compare to first in the winners winners = [i]; //new high score }else if (combinedPointsArray[winner] == combinedPointsArray[i]) { winners[winners.length] = i;//add to highest tie } //danceoff between all numbers in winner array }//for for(i=0;i<winners.length;i++){ document.write(winners[i]+", "); document.write(i); } </SCRIPT> I am having trouble with this program, I cant seem to figure out why this is not working. Basically i need it to loop though the array and find who has the highest points / if more than one have the same points so i dance off is required. Can someone please lend a hand driving me mad. Regards, I am a newbie and am writing a function. I want the function to refer to an arrays elements to get the biggest, but I dont know how to do this. Here is the code: Code: function unique(first,second,third) { var answer; if (first > second) { answer = first - second; } if (second > third) { answer = second - third; } if (third > first) { answer = third - first; } return answer; } //function call unique (digitArray(0,1,2)); what I am trying to do, is get the array elements to be called and give me an answer. Will I need to add the array to the function? It is further down the code after the function at the moment. Or will I need to add the array elements to the function? Hope it is clear Hello im making a search program and the results are displayed in HTML, i decided i wanted a link after each line in an innerHTML div, the problem is i cant set the id dynamicly i have the class is to style. .innerHTML = "<a id = 'myarray[arraycounter]' class = 'innerLink' href = 'javascript:'>View camp information</a>" but this dosent work. i also tried using " thinking that it was because it needed double quotes but this still didnt work. when i inspect the element in chrome it just comes up as array[value]. please help me out guys i need a way to dynamicly give it a link. I 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. /* hi to all... good day to u all... i have a noob question i followed this instruction Code: for (count=1; count<11; count++) { javascript code here } at the bottom of my page i did this Code: <script type="text/javascript"> for (counter=1; counter<11; counter++) { var followTrigger+counter+a = new Spry.Widget.Tooltip('following'+counter+'a', '#followMe'+counter+'a', {followMouse: true}); } </script> what im trying to do is to make multiple of this Code: <script type="text/javascript"> var followTrigger1a = new Spry.Widget.Tooltip('following1a', '#followMe1a', {followMouse: true}); var followTrigger2a = new Spry.Widget.Tooltip('following2a', '#followMe2a', {followMouse: true}); var followTrigger3a = new Spry.Widget.Tooltip('following3a', '#followMe3a', {followMouse: true}); var followTrigger4a = new Spry.Widget.Tooltip('following4a', '#followMe4a', {followMouse: true}); var followTrigger5a = new Spry.Widget.Tooltip('following5a', '#followMe5a', {followMouse: true}); var followTrigger6a = new Spry.Widget.Tooltip('following6a', '#followMe6a', {followMouse: true}); var followTrigger7a = new Spry.Widget.Tooltip('following7a', '#followMe7a', {followMouse: true}); var followTrigger8a = new Spry.Widget.Tooltip('following8a', '#followMe8a', {followMouse: true}); var followTrigger9a = new Spry.Widget.Tooltip('following9a', '#followMe9a', {followMouse: true}); var followTrigger10a = new Spry.Widget.Tooltip('following10a', '#followMe10a', {followMouse: true}); </script> i dont know why it wont work... any help pls... public static void main(String[] args){ int oddNo; oddNo = getOdd(); } public static int getOdd(){ int oddNo = 0; Scanner sc = new Scanner(System.in); while (oddNo % 2 == 0){ System.out.print("Enter an odd number : "); oddNo = sc.nextInt(); System.out.println("Wrong input ! "); } if (oddNo % 2 !=0); System.out.print("The odd number is : " + oddNo); return oddNo; } however, the output: Enter an odd number : 10 Wrong input ! Enter an odd number : 8 Wrong input ! Enter an odd number : 9 Wrong input ! (how to remove this?) The odd number is : 9 I need to loop through an array like this one: [1,2,4,8,8,16,32,64] I don't really know how to do this, so a little guidance would be great. Basically, I have 4 vars: a b c and d in the form of (ax + b)(cx + d) inside the loop something like this: i++ a = array[i] If (a*b > Math.floor(a) && a*b < Math.ceil(a)){ then increment i again and do the loop again} else{ break the loop and set leave a = array[i] } A code sample or something would be really nice (I don't know how to do the parts that are written out in text... Thanks! } hi there, I need to ask my users to input some numbers via textbox(or prompt) using a DELIMITER (,) between each digit. Then The program will display(list) when clicking a button. for this I will use three columns in the first column I have (2,4,6,9) to prompt. in the second column I have (3,4,7,10) to prompt. in the third columns I have (5,6,11,14) to prompt so the first lines should look like: 2,3,5 2,3,6 2,3,11 .... .... .... and the last line : 9,10,14 there should be 4x4x4=64 rows/lines How can we achieve this ? Thanks in advance Hi Guys, I am trying to create a game similar to this https://www.carchallenge2014.com/practice.php What I want to know is how can I run a script so that the script will be able to click on the hands and increase the score. Willing to pay for the help as well. Tks hat i'm trying to achieve is to add two numbers using do while loop. below is the code however result wont show up. i cant seem to determine the error. help me pls. Quote: <script = "text/javascript"> function myanswer(x,y){ x=parseInt(x); y=parseInt(y); i=1; sum=0; do { sum=x+i; i++; } while(i<y) document.myform.asnwer.value=sum; } </script> <form name='myform'> Enter First number:<input type='text' name='x'></br> Enter Second number:<input type='text' name'y'></br> Result:<input type='text' name='answer'></br> <input type='button' value='Click on Me!' onclick="myanswer(myform.x.value,myform.y.value);"</br> </form> Hi, I have this code.. Code: javascript:function showMore() {ProfileStream.getInstance().showMore(); setTimeout(showMore, 2000)}; showMore(); i want to loop it 10 times, but do not know how to do it. I have been told that the for var command is what I want, but I cannot get it to work. Any help would be appreciated. Thanks Code: function funt() { var link = document.getElementsByClassName("class")[0]; if(link != null) { var i = 0; if(check != link.childNodes[i]) { link.childNodes[i].click(); check = link.childNodes[i]; } else { window.location = "website" } } else { if(document.getElementById('enbut').value != "some value") { document.getElementById('id').click(); } window.location = "website" } t=setTimeout("funt()",3000); } with this i'm trying to get it to loop every 3 seconds using the settimeout function at the end of the loop. each time i run this it just runs once and exits. anyone know what i'm doing wrong? Hi ive got this code that hides the productPrice div if the productPrice div inner html is equal to $0.00 So far this works for the first product item on the page but how can i loop this so that it hides every productPrice div on the page? Code: function hide() { var intotal=document.getElementById('productPrice').innerHTML; if (intotal) { intotal=parseFloat(intotal.substring(1).replace(/,/gi,"") ); if (intotal == 0.00) { document.getElementById('productPrice').style.display = "none"; } } } hide(); cheers, help would be very much appretiated I have a script where I can PregReplace soething in a string with a defined value from my input field and it works like a charm. Code: <?php echo '<input id="pfrom" type="text" value="" name="pfrom">'; ?> <script> function demoRegex(){ var txt = document.newform.thestring.value; var re = new RegExp(document.newform.pfrom.value, "g"); newtxt = txt.replace(re,''); alert(newtxt); } </script> But what if I have multiple input field like this: PHP Code: foreach($arr as $arr1=>$value){ echo '<input id="pfrom" type="text" value="" name="pfrom[]">'; } How do I get the javascript to run through all the fields? Hoping for help.... Thanks in advance ;-) We were given a task at school today. Basically we need to have a prompt box popup, and the user enters numbers 1-12 (1 for each month of the year). The prompt keeps popping up until the user types "stop". It will display the months that were entered. Could anyone assist me with this, thanks. What I want to make is an html that loop between 2 websites. so it starts out at website "a" (site "a" does have a timer that will redirect on its own) then it switches to website "b", and that goes on for 30 seconds and then switch back over to website "a" and so on forever. If someone could help me out with this, it would be greatly appreciated First time poster. so be kind Hi All, I have found a bit of shared code on the net that can be used on Facebook. The way it is used is on profiles, or groups, that extend beyond an original page. You load the page and then paste the script into the address bar (in FF and chrome) and hit enter. This then goes down and looks for the 'older post' tag and expands it every 4 seconds. When it gets to the end of the group, it does not stop, it just goes into a loop and restarts from the beginning. I am wanting help to get it to stop at the end of the first pass. This is the code: Code: javascript:function clrt(){alert('Stopped Post Load');if(f_sM){clearTimeout(f_sM);}};function showMore(){ProfileStream.getInstance().showMore();f_sM = setTimeout(showMore, 4000); xsm=document.getElementById('profile_pager_container');xsm.addEventListener('click',clrt,false)}; showMore(); I have also seen it done, with the same looping results, with this code: Code: javascript:function showMore() {ProfileStream.getInstance().showMore(); setTimeout(showMore, 4000)} showMore(); Can anyone help please? Thanks If in my page I have a facebook widget & a pagecounter widget, both I dont want to display on pages other than homepage. I will do 1] Code: <div id="facebook"> .....facebookwidget...</div> <div id="counter">.....counterwidget.....</div> then I will use something like this 2] Code: $(function() { var currentUrl = window.location.href; if(currentUrl != 'http://engineering.forumotion.info/') { $('#facebook').remove(); $('#counter').remove(); } }); However if I use a same id for both widgets say e.g facebook then Code: $(function() { var currentUrl = window.location.href; if(currentUrl != 'http://engineering.forumotion.info/') { $('#facebook').remove(); } }); the above code only removes one(that occurs first) widget & NOT the other widget with the same id. Here we include looping, plz anyone tell me how one start & end a loop, to find a particular class/ID. Plus how the jquery .each() function can be implemented Hi everyone. I'm trying to figure out how to make it so that when someone enters a number in the first textbox to start counting from and then they enter a second number where to stop counting. then the user must enter a number to step up/down by when counting.. there are 3 textboxes, one for start, one for end and one for the step up/down. When the user pushes the button the page clears and then the numbers will show.. If they use the same starting and ending number they have to be alerted that it is not valid. The user can enter a starting number bigger than the ending number. Take into account that a user might enter a positive or negative step value any tips or guidance for this is very appreciated |