gautamshaw Posted September 30, 2009 Share Posted September 30, 2009 I am very new to Java Script.......i tried this code:<script language="javascript">function Dialog_Box(){ var a=confirm("Do you want to continue?") { if(a==true) { var b = prompt("Is 2 a prime number?","Answer") if(b!=null && b!="") { if(b.touppercase()=="Yes") { alert("You are right") } else { alert("You are wrong") } } } } }</script>But its not working properly.........i am having problems with the innermost if-else statement;i.e with this part of my code: if(b.touppercase()=="Yes") { alert("You are right") } else { alert("You are wrong") }If the user enters yes to the to the textbox present in the prompt dilog box then i want to show the alert("You are right") else alert("You are wrong")........i need some help in this.......thanks a lot Link to comment Share on other sites More sharing options...
jeffman Posted September 30, 2009 Share Posted September 30, 2009 uppercase --> YES Link to comment Share on other sites More sharing options...
gautamshaw Posted September 30, 2009 Author Share Posted September 30, 2009 if(b=="YES" || b=="yes") { alert("You are right") } else { alert("You are wrong") }This works for me but in this case if the user enters Yes then You are wrong is displayed,hence i want to use the toUpperCase..........but the way i did this above did not work.......... Link to comment Share on other sites More sharing options...
heldplayer Posted September 30, 2009 Share Posted September 30, 2009 you don't have to insert { before if (condition)...try if it will work if you leave the { and } away from all if statements Link to comment Share on other sites More sharing options...
gautamshaw Posted September 30, 2009 Author Share Posted September 30, 2009 I did exactly this:if(b!=null && b!="") { if(b.toUpperCase=="YES") { alert("You are right") } else { alert("You are wrong") } }This did not work if i enter yes............ Link to comment Share on other sites More sharing options...
gautamshaw Posted September 30, 2009 Author Share Posted September 30, 2009 i removed all the { }Here is my code:<script language="javascript">function Dialog_Box(){var a=confirm("Do you want to continue?")if(a==true)var b = prompt("Is 2 a prime number?","Yes or no?")if(b!=null && b!="") if(b.touppercase=="YES")alert("You are right")elsealert("You are wrong") }</script>Still it did not work properly Link to comment Share on other sites More sharing options...
heldplayer Posted September 30, 2009 Share Posted September 30, 2009 you didn't need to remove all the {'s ad }'s, just the one before the first if statement,look at the following examples, function wrong(){var wrongA=confirm("Is this wrong?");{if(wrongA=="true"){alert("Correct!")}else{alert("Wrong!")}}}function wright(){var wrightA=confirm("Is this wright?")if (wrightA=="true"){alert("Correct!")}else{alert("Wrong!")}} see the difference? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 30, 2009 Share Posted September 30, 2009 <script type="text/javascript">function Dialog_Box(){ var a=confirm("Do you want to continue?") if(a==true) { var b = prompt("Is 2 a prime number?","Yes or no?") if(b!=null && b!="") { if(b.toUpperCase()=="YES") alert("You are right") else alert("You are wrong") } }}</script> Indenting goes a long way towards helping you figure out the structure of the code. Link to comment Share on other sites More sharing options...
jeffman Posted September 30, 2009 Share Posted September 30, 2009 I like braces. They add minimal characters and keep the code tidy. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.