...

View Full Version : Why won't this work?



amster
12-22-2011, 07:42 PM
Hello again,

I have written the following code:


<script language="Javascript">
var win=0
</script>
<Form name="Form">
<Input type="button" name="button" value="Click" onClick="Good()">
</form>
<script language="Javascript">
function Good()
{
if (win=0)
{
alert('You win!')
}
else
{
alert("You lose!")
}
}
</script>

Based on the following, when I click the button it should say "You win!" as win=0 but it keeps saying "You lose!". Why is this happening?

xelawho
12-22-2011, 07:46 PM
if (win==0)

amster
12-22-2011, 07:48 PM
Thanks a lot! Silly me.

Philip M
12-22-2011, 07:51 PM
<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.

amster
12-22-2011, 07:58 PM
Ok thanks! One other question now. I expanded it to this:


<script language="Javascript">
var win=0
setTimeout("Bad()",4000)
</script>
<Form name="Form">
<Input type="button" name="button" value="Click" onClick="Good()">
</form>
<script language="Javascript">
function Good()
{
if (win==0)
{
alert('You win!')
}
else
{
alert("You lose!")
}
}
;
function Bad()
{win==1
}
</script>

Based on the following, I wanted it to say "I win" after clicking the button but if I click the button after 3 seconds, it says "I lose!". What is wrong here as it just keeps telling me "I win!" when I click the button!

EDIT: I've seemed to have solved my problem. When I made it = instead of ==, it worked. So when exactly would I use == or when would I use =?

xelawho
12-22-2011, 08:08 PM
I don't think you have grasped the difference between = and ==

you need to know this.

And once you have changed the value of your variable "win" you need to call the function again to test it...



<script type = "text/javascript">
var win=0
setTimeout("Bad()",4000)
</script>
<Form name="Form">
<Input type="button" name="button" value="Click" onClick="Good()">
</form>
<script language="Javascript">
function Good()
{
if (win==0)
{
alert('You win!')
}
else
{
alert("You lose!")
}
}
;
function Bad()
{win=1
Good();
}
</script>


I don't mean to be rude, but can I suggest some basic tutorials like http://www.w3schools.com/js/ ? Learning the way you are doing it will take a very long time...

amster
12-22-2011, 10:12 PM
Oh not rude at all. I'm actually looking at those tutorials as I go haha. I just sometimes go on tangents and get distracted and start trying other stuff.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum