Jump to content
MakeWebGames

Variables..


Karlos

Recommended Posts

Well I finally decided to learn JavaScript.. But I ran into a few problems along the way and now I'm confused...

Well I decided to use x as my variable but it says on w3schools I need to clear the variable before I can re-assign it to a new value.

Well here's my script what I got:

 

<?php
require_once (DIRNAME(__FILE__) . '/globals.php');
?>

<script type="text/javascript">
<!--

var x = 5;
if (x == 4)
{
document.write("Wrong!");
}
else
{
document.write("Correct!");
}
var x;

document.write("

");

var x = 5;
if (x == 5);
{
document.write("I'm learning");
}
else
{
document.write("Or not....");
}
var x;

//-->
</script>

<?php
$h->endpage();
?>
Link to comment
Share on other sites

Re: Variables..

Well in the end I still had no luck...

So I decided to play around and work with simple arithmetic and if statements and this is what I got..

But i still would like to find the answer out if anybody can help me..

 

<?php
require_once (DIRNAME(__FILE__) . '/globals.php');
?>


<script type="text/javascript">
<!--

var x;
x = 5;
if (x == 4)
{
document.write("X Doesn't Equal 4");
}
else
{
document.write("X Equals 5");
}

document.write("
");

var y;
y = 10;
if (y == 10)
{
document.write("Y Equals 10");
}
else
{
document.write("Y Doesn't Equal 10");
}

document.write("
");

var z;
z = x + y;
if (z = 15)
{
document.write("Z Equals 15...Simple Arithmetic");
}
else
{
document.write("What Planet Are You On....Z Equals 15... -.-");
}

document.write("
");

if (z >= 16)
{
document.write("Z Equals 15...Simple Arithmetic");
}
else
{
document.write("What Planet Are You On....Z Equals 15... -.-");
}


//-->
</script>

<?php
$h->endpage();
?>
Link to comment
Share on other sites

Re: Variables..

 

but it says on w3schools I need to clear the variable before I can re-assign it to a new value.

I'm not sure what w3 was talking about there, but using the keyword "var" multiple times on the same variable does nothing.

It has a special use within functions/objects to set scope, but within the same scope, it's doing nothing for you to use var more than once.

var x = 1;

 

x = 2;

x = 3;

alert(x);

 

--------------

You'll see the alert pop up with "3".

 

hope that helps.

Link to comment
Share on other sites

Re: Variables..

Well as I said I'm learning JavaScript for the first time so I'm not to sure on some stuff and w3schools doesn't explain it a lot in detail but thanks Floydian it helped and my final crappy out come was: (But i hoepfully will learn it better and make more and better scripts in the future.)

<?php
require_once (DIRNAME(__FILE__) . '/globals.php');
?>


<script type="text/javascript">
<!--

var x;
x = 5;
if (x == 4)
{
document.write("X Doesn't Equal 4");
}
else
{
document.write("X Equals 5");
}

document.write("
");

x = 11;
if (x == 9)
{
document.write("X Doesn't Equal 9");
}
else if(x == 11)
{
document.write("X Equals 11");
}
else
{
document.write("X Equals A Number You Dooofus!");
}

//-->
</script>

<?php
$h->endpage();
?>
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...