...

View Full Version : if and else



the_bob
01-17-2004, 01:19 AM
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>

Skyzyx
01-17-2004, 01:38 AM
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.



<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>

the_bob
01-17-2004, 02:33 AM
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>

fredmv
01-17-2004, 03:01 AM
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:
<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/manuals/2000/javascript/1.5/reference/stmt.html#1018610

Skyzyx
01-17-2004, 03:09 AM
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:


<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:


<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).

the_bob
01-17-2004, 03:56 AM
Thanks for the codes! I fredmv's code to works perfectly but for some reason neither of Skyzyx's will work on my site...

glenngv
01-19-2004, 09:39 AM
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!");
}

fredmv
01-19-2004, 03:06 PM
Edited: Nevermind. Glenn said exactly what I said...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum