Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: if and else

  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    if and else

    ok, this is the first time i've ever tried using if and else and i can't get it to work, does anyone see any problems?

    <script>

    var name = prompt("What's your name?","");
    alert(name);
    if (name==Bob)
    alert("Cool! That name rocks!");

    else

    alert("What kind of name is" + name + "?");

    </script>

  • #2
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In your IF statement, you're comparing the variable name to the variable Bob. You want to compare the variable name to the string Bob.

    Code:
    <script type="text/javascript">
    <!--
    var name = prompt("What's your name?", "");
    alert(name);
    
    if (name.toLowerCase() == "bob") {
    	alert("Cool! That name rocks!");
    }
    else {
    	alert("What kind of name is" + name + "?");
    }
    //-->
    </script>

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #3
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. That code works great, but now I'm at another problem, I want more than one name to have an "if" I tried this but it didn't work

    <script type="text/javascript">
    <!--
    var name = prompt("What's your name?", "");
    alert(name);

    if (name.toLowerCase() == "bob") {
    alert("Cool! That name rocks!");
    }

    if (name.toLowerCase() == "oatis") {
    alert(Cool! That name rocks!");
    }

    if (name.toLowerCase() == "todd") {
    alert(Cool! That name rocks!");
    }

    if (name.toLowerCase() == "toddeth") {
    alert(Cool! That name rocks!");
    }

    else {
    alert("What kind of name is" + name + "?");
    }
    //-->
    </script>

  • #4
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's not working because you're missing some quotes for the strings in your alerts. However, the best way to do this would be something like this:
    Code:
    <script type="text/javascript">
    /*<![CDATA[*/
    	var name = prompt('What\\'s your name?', '');
    
    	switch(name.toLowerCase())
    	{
    		case 'bob':
    		case 'oatis':
    		case 'todd':
    		case 'toddeth':
    			alert('Cool! That name rocks!');
    		break;
    		
    		default:
    			alert('What kind of name is ' + name + '?');
    		break;
    	}
    /*]]>*/
    </script>
    http://devedge.netscape.com/library/...t.html#1018610

  • #5
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    fredmv, yes that is a better way, but it doesn't follow the same syntax as the previous question.

    If the point of this exercise is to accomplish entering several names and having it tell you that your name rocks, then fremv's way is better for that.

    If the point is to learn if-else statements, you can do it one of two ways:
    Code:
    <script type="text/javascript">
    <!--
    var name = prompt("What's your name?", "");
    alert(name);
    
    if (name.toLowerCase() == "bob") {
    	alert("Cool! That name rocks!");
    }
    
    else if (name.toLowerCase() == "oatis") {
    	alert(Cool! That name rocks!");
    }
    
    else if (name.toLowerCase() == "todd") {
    	alert(Cool! That name rocks!");
    }
    
    else if (name.toLowerCase() == "toddeth") {
    	alert(Cool! That name rocks!");
    }
    
    else {
    	alert("What kind of name is" + name + "?");
    }
    //-->
    </script>
    ... or you can shorten it up by adding the "or" operator:
    Code:
    <script type="text/javascript">
    <!--
    var name = prompt("What's your name?", "");
    alert(name);
    
    if (name.toLowerCase() == "bob" || name.toLowerCase() == "oatis" || name.toLowerCase() == "todd" || name.toLowerCase() == "toddeth") {
    	alert(Cool! That name rocks!");
    }
    
    else {
    	alert("What kind of name is" + name + "?");
    }
    //-->
    </script>
    P.S. If you wouldn't mind, it'd be easier to read your code if you used the [ code ] BB tags around it (no spaces between brackets).

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #6
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the codes! I fredmv's code to works perfectly but for some reason neither of Skyzyx's will work on my site...

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    It's because the opening double quote in the string in the alert in all "else if" blocks is missing

    else if (name.toLowerCase() == "oatis") {
    alert("Cool! That name rocks!");
    }

  • #8
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Edited: Nevermind. Glenn said exactly what I said...


  •  

    Posting Permissions

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