JavaScript - More Than One Return Statement In A Function? Good Thing/bad Thing
Similar TutorialsI need to know how to subtract a number which selecting an option from checkboxes. As seen in picture below. Basically you have a maximum of 10 stats to select. If you select say 3 Strength, I need the 10 to change to a 7, and it changes on each new value the select boxes have. So when they equal 10, it'd show a red 0 remaining. I'd prefer some way of not allowing more than a maximum of 10 from being selected, but don't know if thats even possible. Thanks for any help! Code: <h2>Starter Stats</h2> <ul> <li>You have <b>10</b> stats remaining.</li><br /> <li> <table width="75%"> <tr><td> <b>Focus</b></td><td><select name="focus"> <option value="0">0</option> <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> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select></td></tr> <tr><td> <b>Attack</b></td><td><select name="attack"> <option value="0">0</option> <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> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select></td></tr> <tr><td> <b>Defense</b></td><td><select name="defense"> <option value="0">0</option> <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> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select></td></tr> <tr><td> <b>Dexterity</b></td><td><select name="dexterity"> <option value="0">0</option> <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> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select></td></tr> <tr><td> <b>Magic Defense</b></td><td><select name="magicdefense"> <option value="0">0</option> <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> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select></td></tr> </table> I know very little JavaScript (I can modify a script slightly, but I can't write one), so I am asking for your help. I want to place a textarea on a web page. This textarea would have attributes to make it so that if you pressed "i", it would come out as "c", or if you pressed "]" it would appear as "=", and so on. (If you're wondering, I want to make it work like the Dvorak keyboard layout). I also would like a button to copy the text in the textarea to the clipboard. Thank you in advance for your help! Jordon HI evey one I want to make a vote music web site , some thing like this : http://www.pmctop20.com/ we can drag each of the items in the left panel and drag it to the vote rows . How can I do so? It's a jquery script Thanks I'm looking for some assistance. I'm trying to write a javascript to validate a form for a class. When I try to submit the form with none of the fields filled in , it goes to the intercept page without showing any errors. In addition to this, I have an error message. Return is outside of function. I have NO clue what they mean here, or what I've done wrong. Could someone take a look and give me an opinion? Enclosed is the entire javascript and the top portion of the related form. <!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> <script type="text/javascript"> <!-- Hides script from older browsers --> "function validate()" { if(document.form2.size.value=="Please Choose") { alert("Did you forget to select a size?"); return false; } if(document.form2.quantity.value=="Please Choose") { alert("How many did you want??"); return false; } if(document.form2.email.value=="") { alert("Please enter your Email Address.."); return false; } if(document.form2.verify.value=="") { alert("Could you enter it again please.."); return false; } if(document.form2.verify.value!=document.form2.email.value) { alert("The Email Addresses do not match"); return false; } if(document.form2.email.value.match((/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)) { alert("Email address was not valid please RETRY"); return false; } else return true; } //--> </script> <form id="form2" name="form2" method="post" onSubmit="return validate(this)" action="2intercept.asp"> <table width="150" border="0" cellpadding="0"> <tr> <td><div align="right"> <label for="size">Size Desired:</label> </div></td> <td><div align="left"> <select name="size" id="size"> <option selected="selected">Please Choose</option> <option value="4x6">4x6</option> <option value="5x7">5x7</option> <option value="8x10">8x10</option> </select> </div></td> </tr> <tr> <td><div align="right"></div> <label for="quantity">Quantity Desired</label></td> <td><div align="left"> <select name="quantity" id="quantity"> <option>Please Choose</option> <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> </select> </div></td> ANY advice, corrections or suggestions would be GREATLY appreciated.. I'm at my wits end with this stuff. when i try to run this it always says INPUT FROM A FILE. a file can have more than one birthday at it will be output also to a file. sorry for my bad english Code: import java.util.*; public class ZodiacSign { public static void main(String[] args) { String sign=""; String element=""; String chineseSign=""; int month, day,year; Scanner c = new Scanner (System.in); System.out.println("Enter day(1-31): "); day = c.nextInt(); System.out.println("Enter month(1-12): "); month = c.nextInt(); System.out.println("Enter year: "); year = c.nextInt(); if((month == 1) && (day <= 20) || (month == 12) && (day >= 22)) { sign = "Capricorn"; } else if((month == 1) || (month == 2) && (day <= 19)) { sign = "Aquarius"; } else if((month == 2) || (month == 3) && (day <= 20)) { sign = "Pisces"; } else if((month == 3) || (month == 4) && (day <= 19)) { sign = "Aries"; } else if((month == 4) || (month == 5) && (day <= 21)) { sign = "Taurus"; } else if((month == 5) || (month == 6) && (day <= 21)) { sign = "Gemini"; } else if((month == 6) || (month == 7) && (day <= 23)) { sign = "Cancer"; } else if((month == 7) || (month == 8) && (day <= 23)) { sign = "Leo"; } else if((month == 8) || (month == 9) && (day <= 23)) { sign = "Virgo"; } else if((month == 9) || (month == 10) && (day <= 23)) { sign = "Libra"; } else if((month == 10) || (month == 11) && (day <= 22)) { sign = "Scorpio"; } else if(month == 12) { sign = "Sagittarius"; } if((sign.equals("Aries"))||(sign.equals("Leo"))||(sign.equals("Sagittarius"))){ element="Fire"; } else if((sign.equals("Taurus"))||(sign.equals("Virgo"))||(sign.equals("Capricorn"))){ element="Earth"; } else if((sign.equals("Gemini"))||(sign.equals("Libra"))||(sign.equals("Aquarius"))){ element="Air"; } else if((sign.equals("Cancer"))||(sign.equals("Scorpio"))||(sign.equals("Pisces"))){ element="Water"; } int x = (1997 - year) % 12; if ((x == 1) || (x == -11)){ chineseSign="Rat"; } else{ if (x == 0){ chineseSign="Ox"; } else{ if ((x == 11) || (x == -1)){ chineseSign="Tiger"; } else{ if ((x == 10) || (x == -2)){ chineseSign="Rabbit"; } else{ if ((x == 9) || (x == -3)){ chineseSign="Dragon"; } else{ if ((x == 8) || (x == -4)){ chineseSign="Snake"; } else{ if ((x == 7) || (x == -5)){ chineseSign="Horse"; } else{ if ((x == 6) || (x == -6)){ chineseSign="Sheep"; } else{ if ((x == 5) || (x == -7)){ chineseSign="Monkey"; } else{ if ((x == 4) || (x == -8)){ chineseSign="Chicken"; } else{ if ((x == 3) || (x == -9)){ chineseSign="Dog"; } else{ if ((x == 2) || (x == -10)){ chineseSign="Pig"; } } } } } } } } } } } } System.out.println("Your Zodiac sign is "+sign+"."); System.out.println("Chinese Zodiac is: "+chineseSign+"."); System.out.println("Element is "+element+"."); } } How could a make a html button that each time your press it it does a new things?
OMG, Check out the background on this site... http://paulirish.com/ how would I even start to rip that for use on my own site? I'll pay someone $15 on paypal if they can make a css and js file that I can just link too and have this background
Code: var c=0; var i=0; function timedCount() { document.getElementById('txt').innerHTML=c; c++; setTimeout("timedCount()",500); } function expand() { while (i<=10) { timedCount(); i++; } } For some reason the innerHTML number starts at 10 because of the while. I want it to start from 0 and stop when at 10... How would I clean up this code? it looks so ugly, would I put it in a for loop and make it loop through and set the rand2, rand3, rand4 = place... there so there is a lot less lines of code(which is what I want) I need a cleaner way to do this, so it isn't so much space. Can someone help? there needs to be different variables because I need the random to be different for each, so rand2 = different random value than rand3. var place = [74,111,148,185,222,259,296,333,370]; var random = [rand,rand2,rand3,rand4,rand5,rand6,rand7,rand8,rand9]; var rand = place[Math.floor(Math.random()*place.length)]; var rand2 = place[Math.floor(Math.random()*place.length)]; var rand3 = place[Math.floor(Math.random()*place.length)]; var rand4 = place[Math.floor(Math.random()*place.length)]; var rand5 = place[Math.floor(Math.random()*place.length)]; var rand6 = place[Math.floor(Math.random()*place.length)]; var rand7 = place[Math.floor(Math.random()*place.length)]; var rand8 = place[Math.floor(Math.random()*place.length)]; var rand9 = place[Math.floor(Math.random()*place.length)]; var newrand = place[Math.floor(Math.random()*place.length)]; var newrand1 = place[Math.floor(Math.random()*place.length)]; var newrand2 = place[Math.floor(Math.random()*place.length)]; var newrand3 = place[Math.floor(Math.random()*place.length)]; var newrand4 = place[Math.floor(Math.random()*place.length)]; var newrand5 = place[Math.floor(Math.random()*place.length)]; var newrand6 = place[Math.floor(Math.random()*place.length)]; var newrand7 = place[Math.floor(Math.random()*place.length)]; var newrand8 = place[Math.floor(Math.random()*place.length)]; var newrand9 = place[Math.floor(Math.random()*place.length)]; var old = place[Math.floor(Math.random()*place.length)]; var old1 = place[Math.floor(Math.random()*place.length)]; var old2 = place[Math.floor(Math.random()*place.length)]; var old3 = place[Math.floor(Math.random()*place.length)]; var old4 = place[Math.floor(Math.random()*place.length)]; var old5 = place[Math.floor(Math.random()*place.length)]; var old6 = place[Math.floor(Math.random()*place.length)]; var old7 = place[Math.floor(Math.random()*place.length)]; var old8 = place[Math.floor(Math.random()*place.length)]; var old9 = place[Math.floor(Math.random()*place.length)]; So I am working on a project to take an integer that the user gives, and then get all the squares and cubes of all the numbers before that to the number given. It displays it in a text area. Maybe I am just lost for the moment, but It just prints the number that I give with the square root and cube instead of starting from 1, and ended on the integer I give. Code: var square_cube = function (){//This f var number = parseInt($("maxbase").value); for (var i=1;i <= number;i++) { var squ = Math.pow(i,2); } var j; for (j=1;j<=number;j++) { var cub = Math.pow(j,3); } var message = i-1 + " " +squ+ " " +cub; $("powers").value = message; } Hello All, i understand there is a certain way to write Pseudocode but i have looked it up on several different websites but i get different hints from each and really do not understand. Any improvements you can think of for the below? i have used the word define alot !! i don't think i should either. Code: <body> <html> <head> <title>Find the Correct Number Game By sam Horne</title> <H1><font color="black">Find the Correct Number Game</font></H1> </body> </html> <SCRIPT LANGUAGE='javascript'> a=(prompt('Please enter your name:',0)); // prompt for player name document.write(" Hello " + a + " and welcome to my game! The aim of the game is to find the correct number which is between 1 and 50. The Higher Lower Indicator will guide you in the right direction. Start by entering any number you wish. Enjoy!!");// print a to screen guessme=Math.round(Math.random()*(50)+1); // generates 1 - 50 truly randomly speech='Choose a number between 1 and 50'; // defined variable function process(hiddennumber) { // defines process guessnumber=document.forms.guesstable.guessnumber.value;// defined variable // defined variable to write to guesstable in guessnumber field speech='"'+guessnumber+ '" Letters are not allowed. Please enter digits only!.'; // defined variable document.forms.guesstable.guessnumber.value=''; // print guessnumber to guesstable form if (guessnumber==hiddennumber)// if guessnumber = hiddennumber Congratulations! hiddennumber is correct! { document.forms.guesstable.prompt.value='Congratulations! '+hiddennumber+' is correct!'; alert ('Congratulations you have guessed the correct number which was'+hiddennumber+'! \n\nIf you want to play again click the button.'); speech=''; document.location=document.location; } if (hiddennumber<guessnumber) // if hiddennumber < guessnumber - lower than guessnumber { speech='Lower than '+ guessnumber; } if (hiddennumber>guessnumber) // if hiddennumber > guessnumber - higher than guessnumber { speech='Higher than '+ guessnumber; } if (guessnumber=='') // if guessnumber is blank - you need to enter a number { speech='You need to enter a number and not leave it blank' } document.forms.guesstable.prompt.value=speech; // document.forms.guesstable.guessnumber.focus(); } </SCRIPT> <FORM onSubmit='' NAME='guesstable'> <CENTER> <TABLE ALIGN='left' BGCOLOR='#000000'> <TR> <TD BGCOLOR='#0000ff'> <FONT COLOR='#ffffff" FACE='comic sans ms'><B>Find the Correct Number</B></FONT> </TD> </TR> <TR> <TD> <CENTER> <INPUT TYPE='text' NAME='prompt' SIZE='50' MAXLENGTH='60' VALUE='Higher Lower Indicator'><BR> // define text length <INPUT TYPE='text' NAME='guessnumber' SIZE='22' MAXLENGTH='2' VALUE='Enter your number here'> //define text length <INPUT TYPE='button' VALUE='Submit' onClick='process(guessme)'> // define button </CENTER> </TD> </TR> </TABLE> </CENTER> </FORM> 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! I'm trying to "progressively enhance" one of my surveys using javascript. Basically, I have rating scales that make use of radio buttons as each point on the scale. Each radio button occupies its own cell in a table. I wrote some functions that will highlight cells on mouseover in a color corresponding to its position on the scale (e.g. the lowest point is red, the midpoint is yellow, the highest point is green). When a radio button is clicked, the background of the button's cell and preceding cells in the same row will be colored accordingly. The functions are working well in FireFox and Chrome (I just have to add a few lines using the addEvent function to make it compatible with IE). The effect looks a lot nicer when I add a function that makes the visibility of the radio buttons hidden. However, I want to make sure that there is a fallback option in case the functions that color the cells don't work for whatever reason. I would not want the radio buttons hidden in this case. Is there a method whereby I can call the "hideRadiobuttons" function only if the other functions are successfully executed? hi,i don't understand about the jquery basic example the code is below PHP Code: <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $('p').addClass(function(n){ return 'par_' + n; }); }); }); </script> <style type="text/css"> .par_0 { color:blue; } .par_1 { color:red; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>Add classes to p elements</button> </body> </html> i want to understand about the how is work function(n){ in the function a and how work return function return 'par_' + n; thanks mate Please help, I have been looking at this all day and I know there must be a simple fix! How do I pass results back to textService so that I can make a call such as textResult = textService(text to pass in); I don't want to use a global variable if I can avoid it. This is the code Code: function textService(text){ req.open("GET", "http://....?text="+text, true); req.onload = showResults; req.send(null); } function showResults() { results = req.responseXML.getElementsByTagName("Result"); } Thank you in advance My function the_magnitude() takes in values from 4 different asp:textboxes. The textboxes "easting" and "northing" contain numbers that don't change, but the textboxes "east" and "north" contain numbers inputed by the user, and the function returns "error" as a value to be put into another textbox that we'll call "error box". So what it's suppose to do is when I update the information in either "east" or"north" it will update, on change, the number in the "error box". It kinda works. My problem is that if the number is a value that changes the style to red, #ff0000, in the function it wont update the "error box" and it just leaves a red zero that is a default number in the box. The only way I can get the number to change is if I end up with a number less than 200, which would make the text green. I'm pretty sure it's a problem with my javascript. here is my function Code: <script type="text/javascript"> function the_magnitude(easting, east, northing, north, error) { var a = easting.value; var b = east.value; var c = northing.value; var d = north.value; var total = Math.sqrt(((a - b) * (a - b)) + ((c - d) * (c - d))); if (total > 200) { document.getElementById('TextBox155').style.color = "#ff0000"; document.getElementById('TextBox155').style.fontWeight = "bold"; } else { document.getElementById('TextBox155').style.color = "#008000"; document.getElementById('TextBox155').style.fontWeight = "bold"; error.value = roundNumber(total); } } </script> and here are the two buttons that I input numbers into for east and north Code: <html> <body> <asp:TextBox ID="TextBox153" runat="server" Height="0.25in" Style="font-family: Arial, Helvetica, sans-serif; font-size: small; text-align: center" Width="0.75in" onchange="the_magnitude(document.getElementById('TextBox153'), document.getElementById('TextBox212'), document.getElementById('TextBox154'), document.getElementById('TextBox213'), document.getElementById('TextBox155'))" ></asp:TextBox> </body> </html> Code: <html> <body> <asp:TextBox ID="TextBox153" runat="server" Height="0.25in" Style="font-family: Arial, Helvetica, sans-serif; font-size: small; text-align: center" Width="0.75in" onchange="the_magnitude(document.getElementById('TextBox153'), document.getElementById('TextBox212'), document.getElementById('TextBox154'), document.getElementById('TextBox213'), document.getElementById('TextBox155'))" ></asp:TextBox> </body> </html> and here is the textbox that I want the return of the function to go into. Code: <html> <body> <asp:TextBox ID="TextBox155" runat="server" Height="0.25in" Style="font-family: Arial, Helvetica, sans-serif; font-size: small; text-align: center" Width="0.5in">0</asp:TextBox> </body> </html> Yesterday I started looking into clientside database handling with html5 and localstorage. I've been trying for hours to make a function like the one below, but I can't figure out how to return the name variable. Any ideas? Code: function hent_kontakt_navn(var1, var2) { var db = MYDB; db.transaction(function (tx) { tx.executeSql('SELECT * FROM db_kontakter WHERE field1=\'' + var1 + '\' AND field2=\'' + var2 + '\' LIMIT 1', [], function (tx, results) { var len = results.rows.length, i; for (i = 0; i < len; i++){ var fornavn = results.rows.item(i).fornavn; var etternavn = results.rows.item(i).etternavn; var navn = fornavn + ' ' + etternavn; alert(navn); //This works and alerts the correct value } }, null); }); return navn; //But this one returns undefined. I know variables don't pass like this between functions, but how can I make it work so that the function returns this var? } I am trying to figure out that if the function "xmlhttp.onreadystatechange=function()" returns true the whole function "function checkUser(str)" should return true as well....so not sure how to do it. i have banged my head since long so please advise! Here is the code below: -------------------------------------------------------- function checkUser(str) { var sp = document.getElementById("msgs"); if (str=="") { document.getElementById("msgs").innerHTML="Username cannot be empty."; return false; } if (str.length <= 6) { document.getElementById("msgs").innerHTML="Username cannot be less than 6 characters and must not start with a number or a special value"; return false; } if (window.XMLHttpRequest) { //code for IE7+, firefox, chrome, opera, safari xmlhttp=new XMLHttpRequest(); } else { //code for old IE xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { if (xmlhttp.responseText === 'FOUND') { sp.style.color = "red"; sp.innerHTML = "[ERROR]: The username \"" + str + "\" already exist. Please try a different name."; return false; } else if (xmlhttp.responseText === 'NOT FOUND') { sp.style.color = "blue"; sp.innerHTML = "Username: VALID"; return true; } } } xmlhttp.open("GET", "checkUser.php?q="+str,true); xmlhttp.send(); } Ok, guys. I'm new to JavaScript, so go easy. I'll be asking bunches of "dumb" questions for a while. 1st question, and the only one for now ... Say I have the following function: Code: function SwapValues(SwapVal1,SwapVal2) { TempSwapVal=SwapVal1; SwapVal1=SwapVal2; SwapVal2=TempSwapVal; } I don't care about the return value of the function itself, but I do need the new values of SwapVal1 and SwapVal2. In ASP when I do this, the values get returned. How do I get this to work in JS? |