JavaScript - Help With Logical Operators Please
I have no idea why my code wont output the corresponding sign. I also have to include a corresponding image with the output and I have NO idea where to even start with that. Heres my code:
Code: <html> <head> <script type="text/javascript"> function signs() { var month; var date; var sign; month=document.zodiac.selMonth.value; date=document.zodiac.selDate.value; sign{ if (month == 1 && date>=20 || month ==2 && date<=18) {value = "You're an Aquarius";} if (month == 2 && date >=19 || month ==3 && date<=20) {value ="pisces";} if (month == 3 && date >=21 || month == 4 && date <=19){value= "Aries";} if (month == 4 && date >= 20 || month == 5 && date <=20) {value= "taurus";} if (month == 5 && date >=21 || month == 6 && date <=21) {value="gemini";} if (month ==6 && date >=22 || month == 7 && date <=22) {value="cancer";} if (month == 7 && date >=23 || month == 8 && date <=22) {value="leo";} if (month == 8 && date >= 23 || month == 9 && date <=22) {value="virgo";} if (month == 9 && date >=23 || month == 10 && date <=22) {value="libra";} if (month == 10 && date >=23 || month ==11 && date <=21) {value="scorpio";} if (month == 11 && date >=22 || month == 12 && date <=21) {value="sagittarius";} if (month == 12 && date >=22 || month == 1 && date <=19) {value= "capricorn";} </script> </head> <body> <form name="zodiacsigns"> <select name"month"> <option value="0">Select the month you were born in</option> <option value="1">january</option> <option value="2">february</option> <option value="3">march</option> <option value="4">april</option> <option value="5">may</option> <option value="6">june</option> <option value="7">july</option> <option value="8">august</option> <option value="9">september</option> <option value="10">october</option> <option value="11">november</option> <option value="12">december</option> </select> <br> <select name"date"> <option value="0">select the date</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> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </select> <br> <input type="button" value="click here to find out your zodiac sign" onClick="signs()"> <br>your sign is: <input type="text" name="zodiacsign" size="12" value=""> </form> </body> </html> Any help would be greatly appreciated, thank you Similar TutorialsHello! Sorry if my question looks ridiculous as I'm completely new to coding stuff. I've downloaded a script from a website and been trying to get it to work for hours now. The desired result should be that when you enter a number between 0 to 40 and press the calculate button, a number (the matching test score) would show up in the box on the right. I would totally appreciate it if anyone here could take a look at it and help me correct it and get it to work? Code: Calculate Your IELTS result - get your Band Score in Reading and Listening <script language="Javascript">// <![CDATA[ function computeGtRead(form) { if (form.cel.value == 1 ) form.farCon.value = 1; else if (form.cel.value >= 2 && form.cel.value <= 7) form.farCon.value = 2; else if (form.cel.value >= 8 && form.cel.value <= 14) form.farCon.value = 3; else if (form.cel.value >= 15 && form.cel.value <= 22) form.farCon.value = 4; else if (form.cel.value >= 23 && form.cel.value <= 29) form.farCon.value = 5; else if (form.cel.value >= 30 && form.cel.value <= 33) form.farCon.value = 6; else if (form.cel.value >= 34 && form.cel.value <= 37) form.farCon.value = 7; else if (form.cel.value >= 38 && form.cel.value <= 39) form.farCon.value = 8; else if (form.cel.value >= 40 && form.cel.value <= 40) form.farCon.value = 9; } function computeAcRead(form) { if (form.cel2.value == 1 ) form.farCon2.value = 1; else if (form.cel2.value >= 2 && form.cel2.value <= 3) form.farCon2.value = 2; else if (form.cel2.value >= 4 && form.cel2.value <= 9) form.farCon2.value = 3; else if (form.cel2.value >= 10 && form.cel2.value <= 15) form.farCon2.value = 4; else if (form.cel2.value >= 16 && form.cel2.value <= 22) form.farCon2.value = 5; else if (form.cel2.value >= 23 && form.cel2.value <= 29) form.farCon2.value = 6; else if (form.cel2.value >= 30 && form.cel2.value <= 34) form.farCon2.value = 7; else if (form.cel2.value >= 35 && form.cel2.value <= 38) form.farCon2.value = 8; else if (form.cel2.value >= 39 && form.cel2.value <= 40) form.farCon2.value = 9; } function computeAcList(form) { if (form.far2.value == 1 ) form.celCon2.value = 1; else if (form.far2.value >= 2 && form.far2.value <= 3) form.celCon2.value = 2; else if (form.far2.value >= 4 && form.far2.value <= 9) form.celCon2.value = 3; else if (form.far2.value >= 10 && form.far2.value <= 15) form.celCon2.value = 4; else if (form.far2.value >= 16 && form.far2.value <= 22) form.celCon2.value = 5; else if (form.far2.value >= 23 && form.far2.value <= 29) form.celCon2.value = 6; else if (form.far2.value >= 30 && form.far2.value <= 34) form.celCon2.value = 7; else if (form.far2.value >= 35 && form.far2.value <= 38) form.celCon2.value = 8; else if (form.far2.value >= 39 && form.far2.value <= 40) form.celCon2.value = 9; } function computeGtList(form) { if (form.far.value == 1 ) form.celCon.value = 1; else if (form.far.value >= 2 && form.far.value <= 3) form.celCon.value = 2; else if (form.far.value >= 4 && form.far.value <= 9) form.celCon.value = 3; else if (form.far.value >= 10 && form.far.value <= 15) form.celCon.value = 4; else if (form.far.value >= 16 && form.far.value <= 22) form.celCon.value = 5; else if (form.far.value >= 23 && form.far.value <= 29) form.celCon.value = 6; else if (form.far.value >= 30 && form.far.value <= 34) form.celCon.value = 7; else if (form.far.value >= 35 && form.far.value <= 38) form.celCon.value = 8; else if (form.far.value >= 39 && form.far.value <= 40) form.celCon.value = 9; } function clearGtForm(form) { form.far.value = ""; form.celCon.value = ""; form.cel.value = ""; form.farCon.value = ""; } function clearAcForm(form) { form.far2.value = ""; form.celCon2.value = ""; form.cel2.value = ""; form.farCon2.value = ""; } // ]]></script> <form method="post"> <div align="center"> <table style="height: 388px;" border="0" width="521" cellspacing="0" cellpadding="4" align="center"> <tbody> <tr> <td colspan="4" align="center" bgcolor="#FFFFF1"> <div align="center"><span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: xx-small;"><big><strong>IELTS Score Calculator (General Training)</strong></big></span></div></td> </tr> <tr> <td style="text-align: center;" colspan="4" align="center" bgcolor="#FFFFF1" height="43"><span style="font-family: arial;">This calculator converts number of correct answers into <strong>approximate</strong> IELTS Band Score </span></td> </tr> <tr bgcolor="#FFFFF1"> <td width="365"><span style="font-family: arial;">Enter number of correct Listening answers (out of 40)</span></td> <td width="36"><input name="far" size="6" type="TEXT" /></td> <td width="30"><input name="button2" type="button" value="Calculate -> " /></td> <td width="47"><input name="celCon" size="6" type="TEXT" /></td> </tr> <tr bgcolor="#FFFFF1"> <td><span style="font-family: arial;">Enter number of correct Reading answers (out of 40) </span></td> <td><input name="cel" size="6" type="TEXT" /></td> <td><input name="button2" type="button" value="Calculate -> " /></td> <td><input name="farCon" size="6" type="TEXT" /></td> </tr> <tr bgcolor="#FFFFF1"> <td style="text-align: center;" colspan="4" align="center">*<input name="reset" type="reset" value="Clear" /></td> </tr> <tr bgcolor="#FFFFF1"> <td colspan="4" align="center"></td> </tr> </tbody> </table> <table style="height: 306px;" border="0" width="521" cellspacing="0" cellpadding="4" align="center"> <tbody> <tr> <td colspan="4" align="center" bgcolor="#FFFFF1"> <div align="center"><span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: xx-small;"><big><strong>IELTS Score Calculator</strong></big></span> <span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: xx-small;"><big><strong>(Academic)</strong></big></span></div></td> </tr> <tr> <td style="text-align: center;" colspan="4" align="center" bgcolor="#FFFFF1" height="43"><span style="font-family: arial;">This calculator converts number of correct answers into <strong>approximate</strong> IELTS Band Score </span></td> </tr> <tr bgcolor="#FFFFF1"> <td width="365"><span style="font-family: arial;">Enter number of correct Listening answers (out of 40)</span></td> <td width="36"><input name="far2" size="6" type="TEXT" /></td> <td width="30"><input name="button" type="button" value="Calculate -> " /></td> <td width="44"><input name="celCon2" size="6" type="TEXT" /></td> </tr> <tr bgcolor="#FFFFF1"> <td><span style="font-family: arial;">Enter number of correct Reading answers (out of 40) </span></td> <td><input name="cel2" size="6" type="TEXT" /></td> <td><input name="button" type="button" value="Calculate -> " /></td> <td><input name="farCon2" size="6" type="TEXT" /></td> </tr> <tr bgcolor="#FFFFF1"> <td style="text-align: center;" colspan="4" align="center">*<input name="reset2" type="reset" value="Clear" /></td> </tr> </tbody> </table> </div> </form> Hello, I try to learn JavaScript and I've just come across something that I can't work out. I use the book of John Pollock: JavaScript, A beginner's guide, Third Edition. So, page 360 and this piece of code: Code: function getname() { var the_text=window.prompt("Enter your first and last name",""); if (the_text.indexOf(" ") == -1) { window.alert("Put a space between your first and last name. Try again."); getname(); } var split_text= the_text.split(" "); if ((split_text[0].charAt(0) != "Z") || (split_text[0].charAt(0) != "z")) { var shorter_fn_string = split_text[0].substring(1,split_text[0].length); new_fn_name = "Z"+shorter_fn_string; } else { var shorter_fn_string = split_text[0].substring(1,split_text[0].length); new_fn_name = "W"+shorter_fn_string; } if ((split_text[1].charAt(0)!= "Z") || (split_text[1].charAt(0)!= "z")) { var shorter_ln_string= split_text[1].substring(1,split_text[1].length); new_ln_name="Z"+shorter_ln_string; } else { var shorter_ln_string= split_text[1].substring(1,split_text[1].length); new_ln_name="W"+shorter_ln_string; } window.alert("Now your name is "+new_fn_name+" "+new_ln_name+"!"); } getname(); the thing is it ain't working. If I type in the propmpt window let's say simon simon I will get an alert of zimon zimon but if I type in zimon zimon it won't change to wimon wimon. I suppouse it's because of != comparison operator. If I use == instead of != and change the code the other way round inside if block then it works. Code: function getname() { var the_text=window.prompt("Enter your first and last name",""); if (the_text.indexOf(" ") == -1) { window.alert("Put a space between your first and last name. Try again."); getname(); } var split_text= the_text.split(" "); if ((split_text[0].charAt(0) == "Z") || (split_text[0].charAt(0) == "z")) { var shorter_fn_string = split_text[0].substring(1,split_text[0].length); new_fn_name = "W"+shorter_fn_string; } else { var shorter_fn_string = split_text[0].substring(1,split_text[0].length); new_fn_name = "Z"+shorter_fn_string; } if ((split_text[1].charAt(0)== "Z") || (split_text[1].charAt(0)== "z")) { var shorter_ln_string= split_text[1].substring(1,split_text[1].length); new_ln_name="W"+shorter_ln_string; } else { var shorter_ln_string= split_text[1].substring(1,split_text[1].length); new_ln_name="Z"+shorter_ln_string; } window.alert("Now your name is "+new_fn_name+" "+new_ln_name+"!"); } getname(); Why is that? Regards, Simon I'm trying to get an emerge effect using javascript. Here is my code Code: var t; var s=0; function emerge() { document.getElementById('my_span').style.opacity=s; s=s+0.1; t=setTimeout("emerge()",250); if (s>1) { clearTimeout(t); } } } Works fine, does the things right. But if I use this code, I get error. Code: var t; var s=0; function emerge() { document.getElementById('my_span').style.opacity=s; s=s+0.1; t=setTimeout("emerge()",250); if (s==1) { clearTimeout(t); } } } The only thing I changed was comparison operator. I tried to debug by adding alert box Code: var t; var s=0; function emerge() { alert(document.getElementById('my_span').style.opacity); document.getElementById('my_span').style.opacity=s; s=s+0.1; t=setTimeout("emerge()",250); if (s>1) { alert("Opacity set to max"); clearTimeout(t); } } } For some reason, it does not enter the if block if I use "==" operator & the alert box keeps coming up with "1" in it. But works fine if I use ">" operator, enters the block and the last value it shows is "0.9". Can anyone explain as to why this happens? Hello! I'd like to ask if it is possible to use conditional operators var = condition ? var1 : var 2 to do the same job as the if-else statements I wrote in red below: Thank you! Code: <p id="text">change text colour</p> <br /> <a onclick="allsorting();">sort in both ascending & descending orders</a> <div></div> <script> /* if & else */ function changetext(){ document.getElementById('text').onclick = function (){ swapcolour(this); } } function swapcolour(text){ if(text.style.color == 'black'){ text.style.color = 'red'; } else {text.style.color = 'black'} } window.onload = changetext; </script> Hi there people I am new to Javascript and have come accross a problem where I need to check the value of a field on a form before it is submitted. It has to be either a whole number OR a particular text value... I have added the following code before the form is submitted. However even if the value of the field is a whole number or it is the correct text string, the message box still pops up and the form fails to submit! Code: if ((document.AddNewVA.HourlyRate.value!='neg') || (isNaN(document.AddNewVA.HourlyRate.value*1))) { // Not the correct text OR a whole number in the HourlyRate field... alert('Please enter a whole number or \"neg\" in the Hourly rate field...'); document.AddNewVA.HourlyRate.value = "0"; document.AddNewVA.HourlyRate.focus(); return false; } Can anyone point me in the right direction please? Many thanks My code isn't working, the only part that is showing up in the webpage is the head. Can someone tell me what I am doing wrong. I typed it right out of the book; the way the book tells me to write it. Thank you. Code: <!DOCTYPE HTML> <html> <head> <title>Comparison Operators</title> </head> <body> <h1>Comparison Operators</h1> <script type="text/javascript"> var conditional Value; var value1 = "Don"; var value2 = "Dave"; value == value2 ? document.write( "<p>value1 equal to value2: true< br />") : document.write( "<p>value1 equal to value2: false<br />") value1 = 37; value2 = 26; conditional value = value1 == value2; document.write("value equal to value2: " +conditionalValue + "<br />"); conditionalValue = value1 != value2; document.write("value1 not equal to value2: " +conditionalValue + "<br />"); conditionalValue = value1 > value2; document.write("value1 greater than value2: " +conditionalValue + "<br />"); conditionalValue = value1 < value2; document.write("value1 less than value2: " +conditionalValue + "<br />"); conditionalValue = value1 >= value2; document.write("value1 greater than or equal to value2: " + conditionalValue + "<br />"); conditionalValue = value1 <= value2; document.write(value1 less than or equal to value2: " + conditionalValue + "<br />"); value1 = 21; value2 = 21; conditionalValue = value1 === value2; document.write( "value1 equal to value2 AND the same data type: " + conditionalValue + "<br />"); conditionalValue = value1 !== value2; document.write( "value1 not equal to value2 AND no the same data type: " + conditionalValue + "</p>"); </script> </body> </html> I've got a slide toggle script online and have got just one bit that I cannot figure out why. What is (toggled = !toggled)? What does it mean? Does that mean toggled = false? But I tested it, it seems not like that. And once the slide is toggled, it should use the minheight. Then the condition for var Height must be the opposite of (toggled = !toggled). Then what is (toggled = !toggled) like? Is that like (toggled != !toggled)? Code: <!DOCTYPE html> <html> <head> <title>Avinash</title> <style> #slider { margin:0px auto; padding:0px; width:400px; border:1px solid #000; background:#0f0; height:0px; overflow:hidden; } </style> <script> var minheight = 0; var maxheight = 1000; var time = 1000; var timer = null; var toggled = false; window.onload = function() { var controler = document.getElementById('slide'); var slider = document.getElementById('slider'); slider.style.height = minheight + 'px'; controler.onclick = function() { clearInterval(timer); var instanceheight = parseInt(slider.style.height); var init = (new Date()).getTime(); var height = (toggled = !toggled) ? maxheight: minheight; var disp = height - parseInt(slider.style.height); timer = setInterval(function() { var instance = (new Date()).getTime() - init; if(instance < time ) { var pos = Math.floor(disp * instance / time); result = instanceheight + pos; slider.style.height = result + 'px'; document.getElementById('log').innerHTML = 'Current Height : <b>' + result + " " + disp + '</b><br /> Current Time : <b>' + instance + " " + pos + '</b>'; }else { slider.style.height = height + 'px'; //safety side ^^ clearInterval(timer); controler.value = toggled ? ' Slide Up ' :' Slide Down '; document.getElementById('log').innerHTML = 'Current Height : <b>' + height + '</b><br /> Current Time : <b>' + time + '</b>'; } },1); }; }; </script> </head> <body> <h1> Toggle Slide </h1> <input type="button" id="slide" value =" Slide Down "/> <span id="log" style="position:absolute; left:10px; top:150px;"></span> <br /> <div id="slider"> content goes here </div> </body> </html> |