JavaScript - Is It Possible To Use An If-else Statement Within A Sentinel Controlled Loop Plan?
Hi there,
I am attempting to set up a very basic menu to drive a program for two different games. I need the menu to process using a primed sentinel controlled loop (where the quit option is the sentinel). I have already set up a function for each game and was hoping to call the function when the user selects a game. I also want to both (a) keep a count of the number of games played and (b) keep a points score, which is to be displayed before the menu is shown agin. When the user quits, I want the final number of points and games played written to document body. I have only very basic knowledge of JavaScript so I realise this is probably a simple answer but I've spent a heap of time on it so far and the menu still has glitches. Any help would be greatly appreciated! This is what I have so far: SENTINEL = 0; MESSAGE_MENU = 'Menu: 1. Pattern Game, 2. Guessing Game, 0. Quit' userInput = parseInt(prompt(MESSAGE_MENU)); while(userInput != SENTINEL) { userInput = parseInt(prompt(MESSAGE_MENU)); if(userInput == 1) { patternGame(); gamesPlayed++; alert('Number of Games Played: '+gamesPlayed) alert('Total Points Scored: '+totalPoints) } else if(userInput == 2) { guessingGame(); gamesPlayed++; alert('Number of Games Played: '+gamesPlayed) alert('Total Points Scored: '+totalPoints) } else { alert('Not a valid input. '+MESSAGE_MENU); } } document.writeln('Total Points: '+totalPoints); document.writeln('Games Played: '+gamesPlayed); Similar TutorialsPlaying with code again and Im sure im skipping something but today im playing with the sentinel controlled loop. which tell me if im wrong is just a while loop until it meets the sentinel? but what say i have three or more options. 1. function#1 2. function #2 3. Exit is there a way to create this? This is what i keep coming up with. Code: // Priming input userInput = parseInt(prompt('0. Exit 1. function 1 2. Function 2')); // Process inputs until sentinel while(userInput != SENTINEL ) { if (userInput ==1) { function1() } else if (userInput ==2) { function2() } document.write('hello') } but obviously when in a while statement its just going to keep looping so what i want to know is there a way to make it work? Thanks Hi Peers, i am trying to build a template for my work. 1- it consists of 3 rows of data pulled from a databse based on 3 parameters entered in a form. those 2 rows includes 60 cells each ( 60 columns : 60 months) 2- i will have another 2 empty rows where i have to call another data from another table or enter it manually.. 3- i ll have the last row that will hold calculation results based on the above rows.. Any idea on how to build that ? \please let me know if you need more details or mock up .. it almost like an excel web spreadsheet except that sopme data is pulled directly from a database.. thank you Hi could someone help me with this code? What is the correct way to write it. Rules: create an array for the user to type 5 strings in a prompt that will be displayed in an alert box. user must type in the prompt an alert will display what the user typed if the prompt is empty or contains nothing an alert will display telling the user to enter text user clicks ok on the alert box and is sent back to the prompt box Code: function display_moreQuotes(){ var quote2 = new Array(5); var y = 0 for(y=0; y<5; y++) { quote2[y]=prompt('Enter your own quote', ' '); alert(quote2[y]); } if((quote2[y]=' ') || (quote2[y]=null)) { alert("Please enter text"); } } Please explain how/where I went wrong. Thanks! Hey guys, I'm working on a javascript seating plan at university (i understand the rules but im not asking for you to do it for me, mearly to help me)... What i've got is a seating plan for a cinema auditorium where there are two types of seats; premium teared seating and regular seating. The premium seats are 10pounds and the regular seats are 5 pounds. I've written the javascript to my ability so far... What i've got just now is the ability to roll over the "available" seats which then does a JS Roll-Over that makes them "mine" seats, and then if you click "Confirm Choices" Button it displays the "mine" seats selected as "taken" seats. It also adds up the seats (10 pounds each). However I've only done the premium seats at the moment and i am unsure of how to do the 5 pound "regular" seats. Here's my javascript code... Code: onload=function(){ myAnchors=document.getElementsByTagName("a"); myImages=document.getElementById("tableA").getElementsByTagName("img"); for (var i=0;i<myAnchors.length;i++){ myAnchors[i].setAttribute('onmouseover','getMine('+i+')'); myAnchors[i].setAttribute('onmouseout','getAvailable('+i+')'); myAnchors[i].setAttribute('onclick','freezeMe('+i+')'); myAnchors[i].setAttribute('id','A'+i); myImages[i].setAttribute('id',i); totalprice =0; } } function getMine(id){ myImages[id].setAttribute('src','images/mine.gif'); } function getAvailable(id){ myImages[id].setAttribute('src','images/available.gif'); } function freezeMe(id){ if(myAnchors[id].getAttribute('onmouseover')==''){ myAnchors[id].setAttribute('onmouseover','getMine('+id+')'); myAnchors[id].setAttribute('onmouseout','getAvailable('+id+')'); } else { myAnchors[id].setAttribute('onmouseover',''); myAnchors[id].setAttribute('onmouseout',''); myImages[id].setAttribute('src','images/mine.gif'); totalprice += 10; } document.getElementById("totalprice").innerHTML = totalprice; } function confirmChoices(){ for (var i=0;i<myAnchors.length;i++){ if(myImages[i].getAttribute('src')=='images/mine.gif'){ myImages[i].setAttribute('src','images/taken.gif'); myAnchors[i].setAttribute('onclick',''); document.getElementById("checkout") } } } function resetAll(){ window.location="home.html"; } and here's my HTML code (there is some styling you can ignore ) Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title> Welcome to Hub Tickets Online </title> <script type="text/javascript" src="scripts/cw1.js"></script> <script type="text/javascript" src="scripts/cw1_2.js"></script> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div id="header"></div> <div id="wrapper"> <div id="navigation"> <img src="images/logo.png"> </div> <div id="myTickets"> <h1>My Tickets</h1> <div id="myTicket"> <h3>Your Seat Selection:</h3> </div> <div id="myPrice"> <table> <tr> <td>Total Price: £</td> <td><div id="totalprice"></div></td> </tr></table> </div> <div id="key"><p><input type="button" id="checkout" value="Check Out" onclick="checkOut()" disabled="disabled" /></p> <p> <img src="images/available_disabled.gif" alt="Available" /> = Available; <img src="images/mine_disabled.gif" alt="Mine" /> = Mine; <img src="images/taken_disabled.gif" alt="Taken" /> = Taken</p> </div></div> <div id="seating"><h4>Raised Premium Seating</h4><h6></h6> <table id="tableA"> <tr> <td colspan="7">A</td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td></td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td colspan="7"></td> </tr> <tr> <td colspan="7">B</td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td></td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td colspan="7"></td> </tr> <tr> <td colspan="7">C</td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td></td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td colspan="7"></td> </tr> </table> <h6></h6> <table id="cheap_seats"><h4>Regular Joe Seats</h4> <tr> <td>D</td><td></td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td></td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td></td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td></td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> </tr> <tr> <td>E</td><td></td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td></td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td></td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td></td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> <td> <a href="#nogo"> <img src="images/available.gif" style="border:none" /> </a> </td> </tr> </table> <h5></h5> <div style="clear:both"> </div> <br /> <div id="theButtons"> <input type="button" value="Confirm Choices" id="confirm" onclick="confirmChoices()" /> <input type="reset" value="Reset" onclick="resetAll()" /> </div> </div> </div> </body> </html> any help would be much appreciated! thanks! Hi All, I need to create a page that is in a sense a Payment Calculator. What it needs to do is take a balance and divide it by a user selected number of months and then display how much must be paid each month to pay off the balance in that time period. How would i set this up to work dynamically so if they decide they want a different number of months it will allow them to change it? I would appreciate any help provided! Thanks Justin Ok, I'm nearly pulling my hair out with this one. I have been looking at this code for two evenings now, and rewrote it 4 times already. It started out as jQuery code and now it's just concatenating strings together. What I'm trying to do: Build a menu/outline using unordered lists from a multidimensional array. What is happening: Inside the buildMenuHTML function, if I call buildMenuHTML, the for loop only happens once (i.e. only for 'i' having a value of '0'.) If I comment out the call to itself, it goes through the for loop all 3 times, but obviously the submenus are not created. Here is the test object: Code: test = [ { "name" : "Menu 1", "url" : "menu1.html", "submenu" : [ { "name" : "menu 1 subitem 1", "url" : "menu1subitem1.html" }, { "name" : "menu 1 subitem 2", "url" : "menu1subitem2.html" } ] }, { "name" : "Menu 2", "url" : "menu2.html", "submenu" : [ { "name" : "menu 2subitem 1", "url" : "menu2subitem1.html" }, { "name" : "menu 2subitem 1", "url" : "menu2subitem1.html" } ] }, { "name" : "Menu 3", "url" : "menu3.html", "submenu" : [ { "name" : "menu 3 subitem 1", "url" : "menu3subitem1.html" }, { "name" : "menu 3 subitem 1", "url" : "menu3subitem1.html" } ] } ]; Here is the recursive function: Code: function buildMenuHTML(menuData,level) { var ul; if (level == 1) { ul = "<ul id='menu'>"; } else { ul = "<ul class='level" + level + "'>"; } for (i = 0; i < menuData.length; i++) { menuItemData = menuData[i]; ul += "<li>"; ul += "<a href='" + menuItemData.url + "'>" + menuItemData.name + "</a>"; if (typeof menuItemData.submenu != 'undefined') { ul += buildMenuHTML(menuItemData.submenu,level + 1); } ul += "</li>"; } ul += "</ul>"; return ul; } Here is how the function is called initially: Code: buildMenuHTML(test,1); This is it's return value (with indentation added for readability): Code: <ul id='menu'> <li><a href='menu1.html'>Menu 1</a> <ul class='level2'> <li><a href='menu1subitem1.html'>menu 1 subitem 1</a></li> <li><a href='menu1subitem2.html'>menu 1 subitem 2</a></li> </ul> </li> </ul> 'Menu 2' and 'Menu 3' don't show up! I'm sure it's something small that I'm overlooking, but any help would be appreciated. Hi all I'm well aware that I can't post assignments here and expect an answer, however, I have been staring at this code for so long. I feel I am close to the solution (to get the correct output to the browser) but I just cannot get it to count how many moves it takes. I don't want an answer, but a nudge in the right direction would be very grateful. As you can see from the code and the output, it will attempt to write to the browser how many moves, but only '0'. Code: function rollDie() { return Math.floor(Math.random() * 6) + 1; } /* *searches for a number in a number array. * *function takes two arguments * the number to search for * the number array to search *function returns the array index at which the number was found, or -1 if not found. */ function findIndexOf(number, numberArray) { var indexWhereFound = -1; for (var position = 0; position < numberArray.length; position = position + 1) { if (numberArray[position] == number) { indexWhereFound = position; } } return indexWhereFound; } //ARRAYS that represent the board -- you do not need to change these //array of special squares var specialSquaresArray = [1,7,25,32,39,46,65,68,71,77]; //array of corresponding squares the player ascends or descends to var connectedSquaresArray = [20,9,29,13,51,41,79,73,35,58]; //VARIABLES used -- you do not need to change these //the square the player is currently on var playersPosition; //play is initially at START playersPosition = 0; //what they score when they roll the die var playersScore; //the index of the player's position in the special squares array or -1 var indexOfNumber; //MAIN PROGRAM //TODO add code here for parts (iii), (iv)(b), (v), and (vi) // start of question(iii) playersScore = rollDie(); document.write(' Sco ' + playersScore); playersPosition = playersScore + playersPosition; document.write(', Squa ' + playersPosition); indexOfNumber = findIndexOf(playersPosition, specialSquaresArray); if (indexOfNumber != -1) { document.write(', Ladder to Squa ' + connectedSquaresArray[indexOfNumber]); playersPosition = connectedSquaresArray[indexOfNumber]; indexOfNumber = -1; } document.write('<BR>') // end of question(iii) // start of question(iv)(b) while(playersPosition<=80) { playersScore = rollDie() document.write(' Sco ' + playersScore) playersPosition = playersPosition + playersScore document.write(', Squa ' + playersPosition) indexOfNumber = findIndexOf(playersPosition, specialSquaresArray) if(indexOfNumber != -1) { document.write(', Ladder to Squa ' + connectedSquaresArray[indexOfNumber]); playersPosition = connectedSquaresArray[indexOfNumber]; } document.write('<BR>'); } var countMoves = 0; while(countMoves <= 0) { document.write('You took ' + countMoves + ' moves to get out'); countMoves = countMoves + 1 } /*for (var countMoves = 0; countMoves < playersPosition; countMoves = countMoves + 1) { countMoves = countMoves + playersPosition; document.write('You took ' + countMoves + ' moves to get out'); }*/ // end of question(iv)(b) // start of question (v) /*if (playersPosition >=80) { document.write('The player is out'); }*/ // end of question (v) </SCRIPT> </HEAD> <BODY> </BODY> </HTML> Many thanks. Hello everyone, I am using javascript and I have a radio button that the user selects and I have a function to see which button was selected, but i'm trying to use the return of that if statement in another statement. Basically another part of the function is dependent on what the user selects in the radio button. Here is what I have so far (I have some things in there that might not work because i'm trying to figure out what works): Code: <script type="text/javascript"> function getSelectedRadio() { len = document.dates.dep.length // returns the array number of the selected radio button or -1 if no button is selected if (document.dates.dep[0]) { // if the button group is an array (one button is not an array) for (var i=0; i<len; i++) { if (document.dates.dep[i].checked) { return i } } } else { if (document.dates.dep.checked) { return 0; } // if the one button is checked, return zero } // if we get to this point, no radio button is selected return -1; } function Calculate() { var i = getSelectedRadio(); if (i == -1) { alert("Please select if Marine entered Delayed Entry Program"); } else { if (document.dates.dep[i]) { return document.dates.dep[i].value; } else { return document.dates.dep.value; } } if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && document.dates.dayDEAF.value < 01 && first return from above ) { document.dates.yearDEP.value = (document.dates.yearDEAF.value); document.dates.monthDEP.value = (document.dates.monthDEAF.value); document.dates.dayDEP.value = (document.dates.dayDEAF.value); document.dates.yearPEBD.value = (document.dates.yearDEAF.value); document.dates.monthPEBD.value = (document.dates.monthDEAF.value); document.dates.dayPEBD.value = (document.dates.dayDEAF.value); } else if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && document.dates.dayDEAF.value < 01 && second return from above ) { document.dates.yearPEBD.value = (document.dates.yearAFADBD.value); document.dates.monthPEBD.value = (document.dates.monthAFADBD.value); document.dates.dayPEBD.value = (document.dates.dayAFADBD.value); document.dates.yearDEP.value = "N/A"; document.dates.monthDEP.value = "N/A"; document.dates.dayDEP.value = "N/A"; } } </script> I color coded in red the returns i'm trying to reference and where they need to be. Is this possible, and if so how can I do it? I haven't been able to find anything on the internet so far. Any help is greatly appreciated! Hello... Thanks for reading... I am getting an undefined error when i try to get a value from this array in the interior loop... Code: // This is the array I am trying to access AuditTable [0] = ["Visio Modifed date","Word Modified Date","User Status","User Comment","Last Audit","Audit Status","Audit Comment"] AuditTable [1] = ["11/23/2009 8:52:18 AM","missing","OK","user comment number 1","1/1/2009","ok","audit comment number 1"] AuditTable [2] = ["11/24/2009 12:21:19 AM","missing","Out of Date","Changes from 2008 not implemented","1/2/2009","Out of Date","needs update"] AuditTable [3] = ["11/22/2009 9:24:42 PM","missing","Incomplete","Document doesnt cover all possibilities","1/3/2009","Inadequate","needs update"] I have hard coded values and had success such as: Code: data = AuditTable[1][0] But when I put the vars associated with the loop in I get an undefined error - AuditTable[i] is undefined: Code: // produces error data = AuditTable[i][j] //Works but retrieves wrong data data = AuditTable[j][i] //Works but retrieves wrong data data = AuditTable[1][i] //Works but retrieves wrong data data = AuditTable[j][2] I must be trying to access the array incorrectly or something... I have defined all the vars, and tried many combinations, alerted the values of both vars so I can prove it is not a scope issue... Am I missing something obvious? Thanks much... Code: var reportArray=new Array(); var reportData, title, subTitle, data; for(i in parmarray)// loop thru AuditTable array and get values { title = '<div style="font-family:verdana; font-size:14px; font-weight:bold; margin:25px 0px 5px 30px">'; title += namearray[i][0]; title += '</div>'; reportArray.push(title);//Take compiled variable value and put it into array for(j=0; j < AuditTable[0].length; j++)// loop thru AuditTable array and get values { subTitle = AuditTable[0][j];//points to first row of AuditTable where the labels are data = AuditTable[1][0];//points to the current row where actual data is html = i + j +'<div style="font-family:verdana; font-size:12px; color:#696969; font-weight:bold; margin-left:30px;">'; html += subTitle; html += '</div><div style="font-family:verdana; font-size:12px; color:#a9a9a9; margin-left:30px; margin-bottom:10px">'; html += data; html += "</div>"; reportArray.push(html);// put results into array } } Hi, I am doing some studying and we was to create a small loop using either the for loop, while loop or do while loop. I chose to do the for loop because it was easier to understand, but I want to know how to do the same using the while loop. Here is the for loop I have, but I cant figure out how to change to while loop. Code: for (var i = 0; i < 5; ++i) { for (var j = i; j < 5; ++j) { document.write(j + ""); } document.write("<br />"); } output is equal to: 01234 1234 234 34 4 How do you make the same using a while loop? it wont loop, as long as you enter something in the name field it will submit. also how do i stop from submitting if all fields are not filled out? any help will be appreciated) ====== function checkForm(form) { var len = form.elements.length; var h=0; for (h=0; h<=len; h++){ if ((form.elements[h].value==null) || (form.elements[h].value=="")){ alert("Please enter "+document.myForm.elements[h].name); document.myForm.elements[h].focus(); return false; } return true; } } ===body-== <FORM NAME="myForm" METHOD="post" ACTION="http://ss1.prosofttraining.com/cgi-bin/process.pl"> Name:<BR> <INPUT TYPE="text" size="30" NAME="name"><br> Email address:<BR> <INPUT TYPE="text" size="30" NAME="email address" onBlur="emailTest(this);"><br> Phone number:<BR> <INPUT TYPE="text" size="30" NAME="phone number"><br> Fax number:<BR> <INPUT TYPE="text" size="30" NAME="fax number"><p> <INPUT TYPE="submit" VALUE="Submit Data" onClick="return checkForm(this.form);"> <INPUT TYPE="reset" VALUE="Reset Form"> </FORM> Code: function showResults( results ) { if ( results.length == 0 ) { msg = "no projects found"; } else {msg = results [0]; } document.getElementById("RESULTS").innerHTML = msg; //= results.join( "<br/>" ); cannot get it to show "no projects found" for 0 results and also to show all multiple results if multiple are found. works right now for 1 result but not multiple. the =results.join( "<br/>" ); works if multiple but then it will not say no projects found for 0 results. Hello CF im wondering how could i use an if statement to have something pop up when visitors visit my site that allows them two choices. it would say something like this. Would you Like to always see the navigation Bar? then they have two options. Yes and No. then depending on what they choose it will either have the code use the absolute or the fixed positioning attribute for the whole code. Or is this just not possible? If you want me to explain it more just ask. No matter what first name and last name I enter I get "Wrong!" I get wrong if I enter john smith or bill shaw Code: <html> <head> </head> <body> <script type="text/javascript"> var input; var first; var last; input = window.prompt("Please Enter First and Last Name (e.g. Joe Smith)"); if ((first == "John" || first == "john") && (last == "Smith"|| last == "smith")){ document.write ("Welcome John Smith"); } if ((first == "Bill" || first == "bill") && (last == "Shaw"|| last == "Shaw")){ document.write ("Welcome Bill Shaw"); } else { document.write("Wrong!"); } </script> <body> </html> I'm not very familiar with java script at all, but could someone please explain how to make an if else else statement that follows these guidelines? 1. If the income is $70,000 or above the tax rate starts at 70% (the % sign will not show) 2. If the income is $20,000 or below the tax rate starts at 10% (the % sign will not show) 3. Otherwise the tax rate starts at 25% Thank you SO much! Hello, I have just started studying JS and don't know a whole lot about it. One exercise I have been given is to use the while statement to ask the user to guess a number between one and ten. this is the code I have attempted so far but it does not work. Simple for any one who is good at this. Impossible to me. Any takershttp://www.codingforums.com/images/smilies/confused.gif <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>While Test</title> <script type="text/javascript"> <!-- function whileTest(){ var number = 4; var answer = 0; while(number != answer ){ answer=prompt("Guesss a number between 1 and ten",""); } alert("4 is the correct answer") //--> </script> </head> <body onLoad="whileTest();"> </body> </html> I want to know how I can update my power_points field if a select option is chosen. For example: if animal guardian is chosen Power_points - (attribute_level * 1) if combined attacks is chosen power_points - (attribute_level * 1) if elemental control is chosen power_points - (attribute_level * 3) etc... Here is my current script: Code: <?php session_start(); ?> <form> <table cellpadding="3" cellspacing="3"> <tr> <th>Attribute</th> <th>Level</th> <th>Power Points:</th> <th>Character Points:</th> </tr> <tr> <td><input type="text" readonly="readonly" value="Scout/Knight Powers" size="19" name="attribute1" /></td> <td><select name="level" onclick="power(), remain()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select></td> <td align="center"><input type="text" name="power_points" id="power_points" size="1" readonly="readonly"/></td> <td align="center"><input type="text" name="cp_remain" id="cp_remain" size="1" readonly="readonly" /></td> </tr> </table> </form> <form method="POST" action="test_batosai.php"> <table><tr><td> <div id="dynamicInput1"> <table cellpadding="3" cellspacing="3"> <thead> <tr> <th>Sub-Attribute</th> <th>Level</th> <th>Notes <i>(optional)</i></th> </tr> </thead> <tbody> <tr> <td> <select name="attribute[]" size="1"> <option value="1">Animal Guardian</option> <option value="2">Combined Attacks</option> <option value="3">Elemental Control</option> <option value="4">Emotional Control</option> <option value="5">Item Of Power</option> <option value="6">Knight Attack</option> <option value="7">Rejuvenation</option> <option value="8">Sailor Scout Attack</option> </select></td> <td> <select name="attribute_level[]"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select></td> <td><textarea name="sub_notes[]" rows="1" cols="30"> </textarea></td> </tr> </tbody> </table> </div> <div id="cloneDiv1"> </div> </td> <td> <div id="dynamicInput2"> <table cellpadding="3" cellspacing="3"> <thead> <tr> <th>Neutral Attribute</th> <th>Level</th> <th>Notes <i>(optional)</i></th> </tr> </thead> <tbody> <tr> <td> <select name="attribute2[]" size="1"> <option value="1">Acrobatics</option> <option value="2">Appearance</option> <option value="3">Art Of Distraction</option> <option value="4">Combat Mastery</option> <option value="5">Damn Healthy!</option> <option value="6">Divine Relationship</option> <option value="7">Energy Bonus</option> <option value="8">Extra Attacks</option> <option value="9">Focused Combat</option> <option value="10">Fortified Body</option> <option value="11">Heightened Negaverse Power</option> <option value="12">Heightened Senses</option> <option value="13">Heightened Scout Powers</option> <option value="14">Massive Damage</option> <option value="15">Powerful Mind</option> <option value="16">Special Attack/Defense</option> <option value="17">Speed</option> <option value="18">Strong Soul</option> <option value="19">Supernatural Training</option> <option value="20">Unique Character Attribute</option> </select></td> <td> <select name="attribute_level2[]"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select></td> <td><textarea name="neutral_notes[]" rows="1" cols="30"> </textarea></td> </tr> </tbody> </table> </div> <div id="cloneDiv2"> </div> </td></tr></table> <input type="button" value="Add another Sub-Attribute" onClick="addInput('dynamicInput1','cloneDiv1');"> <input type="button" value="Add another Neutral Attribute" onClick="addInput('dynamicInput2','cloneDiv2');"> <input type="submit" /><input type="reset" /> </form> <script type="text/javascript"> var counter = 1; var limit = 10000000;// Set a limit if you want to function addInput(divFrom,divTo){ if (counter == limit) { alert("You have reached the limit of adding " + counter + " inputs"); } else { var newdiv = document.createElement('div'); newdiv.innerHTML = document.getElementById(divFrom).innerHTML; document.getElementById(divTo).appendChild(newdiv); counter++; } } //function for Power Points function power() { var first,res1; //Take the value of first textbox and convert it to float first=parseFloat(document.forms[0].level.value); res1=(first)*10; //show the result in the result textbox document.forms[0].power_points.value=res1; } //end function for Power Points //function for Character Points remaining function remain() { var first,res2; //Take the value of first textbox and convert it to float first=parseFloat(document.forms[0].level.value); res2=10-((first)*4); //show the result in the result textbox document.forms[0].cp_remain.value=res2; } //end function for Character Points </script> Can anyone help? I've got this code below to change an images source url based on the clicks from a dropdown menu. Is it possible to shorten this with some sort of if/or statement instead of all the else if's? something that says "if crenate or serrate or this or that, do this" here's the function: Code: function change3(picName,choice) { var url = (document[picName].src); if (/crenate\D{1}/.test(url)) { url = url.replace(/crenate/, choice) document[picName].src=(url); } else if (/margin_finelyserrate\D{1}/.test(url)) { url = url.replace(/margin_finelyserrate/, choice) document[picName].src=(url); } else if (/margin_lobed\D{1}/.test(url)) { url = url.replace(/margin_lobed/, choice) document[picName].src=(url); } else if (/margin_undulate\D{1}/.test(url)) { url = url.replace(/margin_undulate/, choice) document[picName].src=(url); } else if (/doubleserrate\D{1}/.test(url)) { url = url.replace(/doubleserrate/, choice) document[picName].src=(url); } else if (/leaf_shapes\D{1}/.test(url)) { url = url.replace(/leaf_shapes/,"leaf_shapes/" + choice) document[picName].src=(url); } } The dropdown menu just has options written like this: Code: <li id="margin1"><a href="javascript:passit4('entire')" onMouseover="change1('pic8','image34')" onclick="ShowContent('uniquename3'); change3('pic2','entire');" onMouseout="change1('pic8','image_off');">sample link (entire)</a></li> can someone help me with this pls: Code: function anything() { var tegenover = document.getElementById("minlas"); var septo = document.getElementById("combo_2"); //prijs verbinder per meter var serr = document.getElementById("comba_2"); //prijs verbinder per meter var seprr = document.getElementById("combb_2"); //prijs verbinder per meter var septrr = document.getElementById("combc_2"); //prijs verbinder per meter var nocho = document.getElementById("wide"); //breedte band in mm document.getElementById("totprijslas").value = Math.round(100*((parseFloat(septo.value) + parseFloat(serr.value) + parseFloat(seprr.value) + parseFloat(septrr.value)) * parseFloat(nocho.value))/1000)/100; } the value returned of totprijslas can be anything between 0 and 1000,but i want the value of "totprijslas" to be a minimum of "50",so for example if result is 30 then i need it to show 50.can someone help me with an if statement for this.thx I am relatively new to JavaScript coding, and I a having some problems with an IF statement. I want the IF statement to tell me when the number is below the minimum. Code: function main (txtpamphletMin,txtpamphletMax,optQuality,selIncrement,txtResults) { var txtpamphletMin,txtpamphletMax,optQuality,selIncrement; var amount = 100; var errorMsg = ""; var results = ""; pamphletMin = pamphletMin.value; //Make sure that the minimum number of pamphlets required is entered if (pamphletMin == "pamphletMin<100") { errorMsg = "Number of pamphlets must be above 100"; } else { pamphletMin = txtpamphletMin.value; //get the number of pamphlets from the text box errorMsg = validatepamphletMin(Minimum Pamphlets); } I know that this is no the entire statement but I am just having problems with this one. Can anyone help? |