www.webdeveloper.com
Results 1 to 8 of 8

Thread: If If Else Else statment

  1. #1
    Join Date
    Dec 2013
    Posts
    7

    If If Else Else statment

    Else if is working fine, Else is working fine as well, but the initial if statement is not working. Can someone share what this noob is doing wrong - thanks in advance

    <code>
    <HTML>
    <HEAD>
    <SCRIPT>
    var age = parseInt(prompt("Please enter your age:"))
    if (age < 21)
    {
    alert("Sorry, you are too young to enter")
    }
    else if (age > 21)
    {
    alert("Sorry, you are too old to enter")
    }
    else (age == 21)
    {
    alert ("Welcome to our exclusive club");
    location.href = "http://www.houstonian.com"
    }

    </SCRIPT>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>
    </code>

  2. #2
    Join Date
    Oct 2013
    Posts
    610
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    	<meta charset="utf-8" />
    	<title>your page title here</title>
    </head>
    <body>
    	<script>
    		var age = parseInt(prompt("Please enter your age:"));
    		if (age < 21){
    			alert("Sorry, you are too young to enter");
    			location.href = "http://www.google.com";
    		}
    		if (age > 21){
    			alert("Sorry, you are too old to enter");
    			location.href = "http://www.google.com";
    		}
    		if (age == 21){
    			alert ("Welcome to our exclusive club");
    			location.href = "http://www.houstonian.com";
    		}
    	</script>
    </body>
    </html>

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443

    Lightbulb

    Two other alternatives...

    1st based upon posted code...
    Code:
    <HTML>
    <HEAD>
    <SCRIPT>
    var age = parseInt(prompt("Please enter your age:",''));
    var tooOldYoung = true;
    if (age < 21) {
      alert("Sorry, you are too young to enter");
    } else {
      if (age > 21) {
        alert("Sorry, you are too old to enter");
      } else { tooOldYoung = false; }
    }
    if (!tooOldYoung) {       
      alert ("Welcome to our exclusive club");
      location.href = "http://www.houstonian.com";
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>
    2nd with a different approach...
    Code:
    <HTML>
    <HEAD>
    <SCRIPT>
    var age = parseInt(prompt("Please enter your age:",''));
    var tooOldYoung = (age < 21) ? -1 : (age > 21) ? 1 : 0;
    switch(tooOldYoung) {
      case -1 : alert("Sorry, you are too young to enter"); break;
      case  1 : alert("Sorry, you are too old to enter"); break;
      default : alert ("Welcome to our exclusive club");
                location.href = "http://www.houstonian.com";
                break;
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Or a third approach :P

    index.html
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="utf-8" />
    	<title>If If Else Else statment</title>
    </head>
    <body>
    	<script type="text/javascript">
    
    		var age = parseInt(prompt("Please enter your age:"), 10);
    		if (age === 21) {
    			alert("Welcome to our exclusive club");
    			location.href = "http://www.houstonian.com";
    		} else {
    			alert("Sorry, you are too " + (age < 21 ? 'young' : 'old') + " to enter");
    		}
    
    	</script>
    </body>
    </html>
    Last edited by bionoid; 12-23-2013 at 01:17 PM.

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,517
    Ooooooh, now here we have a blatant case of age discrimination going on!
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Quote Originally Posted by \\.\ View Post
    Ooooooh, now here we have a blatant case of age discrimination going on!
    Don't worry \\.\, I'm not allowed in either

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443
    Quote Originally Posted by bionoid View Post
    Don't worry \\.\, I'm not allowed in either
    Too young?

  8. #8
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Quote Originally Posted by JMRKER View Post
    Too young?
    hahahahahaha, OH I WISH!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles