JavaScript - Comparison Operators !=/== Don't Work
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 Similar TutorialsMy 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'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? 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 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 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> Why alert('hua'); does not pop up ? Code: var cur_date = new Date(); cur_date.setSeconds(0); cur_date.setHours(0); cur_date.setMilliseconds(0); cur_date.setMinutes(0); alert(cur_date); alert(new Date(cur_date.getFullYear(),cur_date.getMonth(),cur_date.getDate())); if (new Date(cur_date.getFullYear(),cur_date.getMonth(),cur_date.getDay()) == cur_date ) { alert('hua'); } are these right ya? '3' !=='smith' true "test" == "test " true '2' != 'smith' true "exam" == "Exam" true 5 === "5 " false "t" > "T" false "y" >= "p" true "Happy" == "happy " true I would like to compare dates in the format used in twitter, which is this: Wed Apr 08 14:30:10 +0000 2009 How do I do this? Do I need to convert to a timestamp first, and if so, how do I do this? G i have an iframe on my webpage and i am loading appropriate page in it by clicking appropriate button but when user logs out and at that time some page is opened in iframe corresponding to the login pages(like "edit profile") of user then at that moment i want to know that what page is loaded in iframe so that i close it(if it corresponds to pages of login like "edit profile") as user logs out and if it does not correspond to login pages then it remains as it is.For doing that i must know that what page is loaded in iframe and compare it in " if(condition){statement} in logout function" with the logion pages so that i know that if any of them is loaded in iframe and if loaded then close it.Can you help me with that by giving exact code example. Hello everyone, for starters, I'm NOT working with arrays...with that being said, I need your help...I created a webform in PHP that retrieves values from a mysql table and displays them with its own mysqli_fetch_array command, in that loop it generates a textbox for each record...so far so good. The created textbox (input element) is so that the user can type in the sequential number of how to reorder the records...example Original Order of Records 1 Alpha 2 Bravo 3 Charlie 4 Delta 5 Echo and user needs it to be in this order User input sequential 2 Alpha 5 Bravo 1 Charlie 4 Delta 3 Echo the new order of the records will be saved on a temporary table in the database before insertion on the main table, kinnda like a preview for the new order, something like this: New Order of Records 1 Charlie 2 Alpha 3 Echo 4 Delta 5 Bravo Now what I need is a function that helps me display a message if the user duplicates a sequential unique, if they type in number 1 in 2 or more records, when I hit the button for the preview I need it to loop through all the input boxes and check their values, compare it with the other inputs and determine if there are duplicates or not....if there're no duplicates, continue with PHP code.....if there are duplicates, display an error so the user seeks for the duplicate and change it (inputs left in blank will not be considered for the insertion in the preview table) I already have this: Code: function checkall() { const t='texto'; var contar=<?php echo $contar; ?>; var text = "" var conta = 1; var contas= conta+1; text = t+conta; texto = t+contas; //var curElement = document.activeElement.value; var cv=document.forms['OrderPreview'][text].value; //var cv=document.forms['OrderPreview'][curElement].value; do { //var cv=document.forms['OrderPreview'][text].value; if (document.forms['OrderPreview'][texto].value=="") { return; } if (cv==document.forms['OrderPreview'][texto].value) { alert("Something Bad"); } conta++; contas++; text = t+conta; texto = t+contas; } while (conta<=contar) } Btw, the document.activeElement part of the code displays me "undefined" in the alert message, I still don't have a clue why, could you guide me with this please? I can't do but compare the 1st input with the rest of the fields, OR compare one field to the inmediate next field....it's driving me nuts Any help will be truly appreciated, thanks in advance 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> I have a drop down menu where people can select a month, day and year. Based on their selection, I want to show them an image. If their selection is >= July 26, 2010 but <= July 25, 2011, show the red image; If their selection is >= July 26, 2011 but <= July 25, 2012, show the white image; If their selection is >= July 26, 2012 but <= July 25, 2013, show the blue image; If their selection is >= July 26, 2013 but <= July 25, 2014, show the yellow image; I don't know how to compare a selected date to a range of dates like this. Hello all, I made a fade script that will fade any element in or out. Works great on all browser I've tested but IE 7. With IE I have only tested this will IE 8 and IE 7. IE 7 the effect doesn't work. No error message or anything. I'm unsure what to do from here. I was hoping I could find some help here. Code: function fade(obj, duration, toggle) { steps = 1000; elem = document.getElementById(obj); function fadeIn() { for(var i = 0; i <= 1; i+=(1/steps)) { setTimeout("elem.style.opacity = "+ i +"", i * duration); setTimeout("elem.style.filter='alpha(opacity="+ i * 102 +")'", i * duration); } } function fadeOut() { for(var i = 0; i <= 1; i+=(1/steps)) { setTimeout("elem.style.opacity = "+ (1-i) +"", i * duration); setTimeout("elem.style.filter='alpha(opacity="+ (1-i) * 102 +")'", i * duration); } } /* One for Fade in and anything will be fade out*/ if(toggle == 1) { fadeIn(); } else { fadeOut(); } } Thanks, Jon W HI i have a form and submit bottom in my page . I've wrote a function that check the page element's value before submitting the form. my problem is this i couldn’t stop submitting the form before it has checked . Actually I want before the form submitted it has checked and if there was no problem then the function return true and submitted and if there was any problem, the function return false and the form stopped submitting I ‘ve added a function to the onclick of submit button and it call a function that check the values thanks hello, I found this script : http://roshanbh.com.np/2008/06/accor...ng-jquery.html I implemted this into my website http://test.tamarawobben.nl but as you can see it don't work. Can anyone help me figure out why the script does not work. Roelof Hello again, I have written the following code: <script language="Javascript"> var win=0 </script> <Form name="Form"> <Input type="button" name="button" value="Click" onClick="Good()"> </form> <script language="Javascript"> function Good() { if (win=0) { alert('You win!') } else { alert("You lose!") } } </script> Based on the following, when I click the button it should say "You win!" as win=0 but it keeps saying "You lose!". Why is this happening? Can somebody give me a bit of help with the below code. The problem is that it works fine for the first pop up box but not for the other(s). I think it has something to do with the script using the getelementbyid. Can anybody help? Code: <script type="text/javascript"> var appearance = "notShown" function show() { if (appearance == "notShown") { document.getElementById('P1').style.display = "inline"; appearance = "shown"; } } function remove() { document.getElementById('P1').style.display = "none"; appearance = "notShown"; } </script> <style type="text/css"> p#P1 { width: 319px; height: 169px; display:none; background-color: grey; position:absolute;z-index:2; } p#P1 b { padding: 10px; float: left; } p#P1 span { width: 300px; float: left; padding: 10px;} button.close {float:right; margin: 5px; } </style> <a href="#" onclick="show()">box 1</a> <p id="P1"><b>Lorem Ipsum</b><button type="button" class="close" onclick="remove()">X</button> <span>Lorem ipsum ut volumus antiopam pericula vix, per nulla oblique alienum ad. No ullum convenire eos. Sit tota iusto ut, ex mentitum voluptatum inciderint est. Ex dico idque argumentum eam, ei elit meliore definiebas per, nam soleat aliquando ad.</span> </p> <br /><br /><br /><br /><br /><br /> <a href="#" onclick="show()">box 2</a> <p id="P1"><b>Lorem Ipsum</b><button type="button" class="close" onclick="remove()">X</button> <span>Lorem ipsum ut volumus antiopam pericula vix, per nulla oblique alienum ad. No ullum convenire eos. Sit tota iusto ut, ex mentitum voluptatum inciderint est. Ex dico idque argumentum eam, ei elit meliore definiebas per, nam soleat aliquando ad.</span> So here is the story I paid for some to create a slideshow, and it works fine in the orginial html document but when i copy and paste the code into my html document the scroller to the left of the image stops moving up and down I put the working file at this address: http://rusty813.comuv.com/ my files at this address where i am: http://rusty813.comuv.com/sample2.html the guy i paid to do work refuses to help even those i advertised the price in my ad and he wanted me to give it to pay him more to tell which code to change but go ahead take a look at the coding thanks alot for your guys help |