...

View Full Version : really basic if else problem



hade911
07-03-2011, 02:21 AM
ok so i just started learning javascript and i know next to nothing and i wasnt surprised when what i wrote didnt work. so i wonder if you guys can help me figure out whats the problem.

<html>
<head>
<script>
var a= prompt();
if (var a>1)
{
alert("I hate 1s!");
}
else
{
alert("I like you!");
}
</script>
</head>

<body>


</body>
</html>


so why doesnt this work?
thanks for the help
james.

Fugix
07-03-2011, 03:31 AM
number one because the prompt() function takes two parameters and returns a string, not an interger
Also in your script tag, you need to specify what type the script is for the parser


<head>
<script type="text/javascript">
<!--
function prompter() {
var reply = prompt("Hey there, good looking stranger! What's your name?", "")
alert ( "Nice to see you around these parts " + reply + "!")
}
//-->
</script>
</head>
<body>
<input type="button" onclick="prompter()" value="Say my name!">
</body>

hade911
07-03-2011, 03:53 AM
thank you for you code, but so can I actually make it ask me hows my day and when i reply good or bad to a prompt, it displays the relevant message?

so heres what i got:

<html>
<head>
<script>
function prompter() {
var a= prompt(" How are are today? good? bad?")
if (var a= good)
{
alert("Thats nice to hear!");
}
else
{
alert("I am sorry, hope you enjoy the rest of your day!");
}
}
</script>
</head>

<body>
<input type="button" onclick="prompter()" value="How are you today, Sir?">

</body>
</html>


is it possible that this can work that way, or am i just barking up the wrong tree?
thanks!

devnull69
07-03-2011, 08:55 AM
You should start learning the basics

1. Prompt returns a string. String literals have to be enclosed in single or double quotes
2. The compare operator is == and not =
3. if you want to compare an existing variable you must not use the var keyword


if(a=="good")

hade911
07-03-2011, 09:00 AM
oh thank you so much :D we were learning this at school and i kinda forgot! thanks that cleared it all up!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum