JavaScript - Looping Through An Array To Find Highest Value
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, Similar TutorialsSo if I have three dollar amounts like .... a = 1,222.00; b = 1,323.00; c = ; d = 1,222.00; How can I alert the highest variable even though there may be 1 or more of them? 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? Hi, 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. I have an array "arr" that is an array of objects. Each object has the same 7 properties. I want to find the index of the object with a property that matches a certain value x in the array arr. How can i accomplish this? The array has hash tables associated with it. arr [ obj [ i ] . property1 + "_" + obj [ i ] . property2 ] = arr [ i ] ; so whats the index of the object where .property1 = x ? Thanks, g 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. /* hey there. the script im trying to write is used to take the values from four separate forms and display an image based on the highest value. it looks like this. Code: <script type="text/javascript"> var val = Math.abs(document.getElementById("mytext1").value); var val2 = Math.abs(document.getElementById("mytext2").value); var val3 etc var val 4 etc var showImg = document.getElementById("image1").src = "default.jpg" document.getElementById("image1").src = "default.jpg" // check + values for right sph if (val >= .25 && val <= 3.5) {document.getElementById("image1").src = "r39.gif"} if (val >= 3.75 && val <= 5.50) {document.getElementById("image1").src = "pol.gif"} // check - values for right cy if (val <= -.25 && val >= -3.5) {document.getElementById("image1").src = "r39.gif"} if (val <= -3.75 && val >= -5.5) {document.getElementById("image1").src = "pol.gif"} // check + values for left sph if (val2 >= .25 && val2 <= 3.5) {document.getElementById("image1").src = "r39.gif"} if (val2 >= 3.75 && val2 <= 5.50) {document.getElementById("image1").src = "pol.gif"} // check - values for left cy if (val2 <= -.25 && val2 >= -3.5) {document.getElementById("image1").src = "r39.gif"} if (val2 <= -3.75 && val2 >= -5.5) {document.getElementById("image1").src = "pol.gif"} etc etc } </script> <input type = "text" id = "mytext1" > <input type = "text" id = "mytext2" /> <input type = "text" id = "mytext3" > <input type = "text" id = "mytext4" > <img src="calculate.gif" onclick="showImg()"> anybody know how i go about getting the highest value checked and the resulting image displayed. thanks in advance. I'm having a hard time trying to get this script to work. I am trying to write a script that finds the highest of 5 numbers and places the highest number in the last textbox. Can anybody tell me what I am doing wrong? Code: <HTML> <HEAD> <TITLE>COMSC-100-1241 - Assignment #10 - Using "Greater Than" And "Less Than" Logic</TITLE> <SCRIPT TYPE="text/JavaScript"> function Go(code){ var number1 var number2 var number3 var number4 var number5 var highest var max number1 = parseFloat(document.getElementById("number1").value); number2 = parseFloat(document.getElementById("number2").value); number3 = parseFloat(document.getElementById("number3").value); number4 = parseFloat(document.getElementById("number4").value); number5 = parseFloat(document.getElementById("number5").value); max = parseFloat(document.getElementById("max").value); if (code == 0){ } if (max < number1){ max = number1 } if (max < number2){ max = number2 } if (max < number3){ max = number3 } if (max < number4){ max = number4 } if (max < number5){ max = number5 } document.getElementById("max").value = highest } </SCRIPT> </HEAD> <BODY> <TABLE BORDER="1" WIDTH="500" ALIGN="center"> <TR> <TD> <DIV ALIGN="left"> <B>Instructions: </B> Answer the three questions and press "GO". Your score will appear. <BR> <BR> <B>Input Values: </B> <BR> <B>First Number:</B> <INPUT id="number1"> <BR> <B>Second Number:</B> <INPUT id="number2"> <BR> <B>Third Number:</B> <INPUT id="number3"> <BR> <B>Fourth Number:</B> <INPUT id="number4"> <BR> <B>Fifth Number:</B> <INPUT id="number5"> <BR> <INPUT TYPE="Submit" VALUE="GO!" onClick="Go(0);"> <BR><BR> <B>Output Values: </B> <BR> <B>Highest Number:</B> <INPUT id="highest"> </FORM> </DIV> </TD> </TR> </TABLE> </BODY> </HTML> Hi i need to find the highest average from all of the averages entered in this code, but i have to use an if statement only, no arrays. Could some one help please? Code: // ** Work out average ** // average= readingSum/totalReadings // ** Put average into a category and display everything ** // if(average<5) { catergory1=alert("Location is " + region + "\nNumber of weekly readings taken is " + totalReadings + "\nTotal of readings is" + readingSum + "\nAverage is" + average + "\nLocation is Dry"); } else if(average>=5 && average<20) { catergory2=alert("Location is " + region + "\nNumber of weekly readings taken is " + totalReadings + "\nTotal of readings is" + readingSum + "\nAverage is" + average + "\nLocation is Normal"); } else if(average>20) { catergory3=alert("Location is " + region + "\nNumber of weekly readings taken is " + totalReadings + "\nTotal of readings is" + readingSum + "\nAverage is" + average + "\nLocation is Wet"); } // ** Find Highest average ** // Thanks. Hey friends, I'm writing a program but I'm having issues. I need to write a program that asks for 3 different numbers (prompt boxes) and determine which number entered was the highest and the 2nd highest. I know what to do for the input and output but I don't know what to do during the processing.
I've written a program which will prompt for a number of cities, Prompt for the name of the city, then prompt for the number of snowfall readings of that city, and then prompt for each of these individual snowfall readings of that city. From this, it adds up each of the individual snowfall readings of that city, and will calculate an average by dividing this figure [the total snowfall] by the total number of readings for that city. This average is used to then classify the city as "not snowy", "mild", or "blizzard". I'm happy to PM my code to anyone willing to help out, as I realise this is a complex structure to visualise perhaps, but I can't post it publicly. I now have to select the city with the highest average, and name it in an alert output. How is this possible. Note, I'm not allowed to use functions or arrays. Prompts, alerts, if-then-else, for and while loops are all I can use. 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 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... 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 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 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 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 ;-) 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? 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> |