Karlos Posted January 6, 2009 Share Posted January 6, 2009 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(); ?> Quote Link to comment Share on other sites More sharing options...
Lithium Posted January 6, 2009 Share Posted January 6, 2009 Re: Variables.. i don't know about it though i found an article that somehow might point you in the right direction http://blog.quomon.com/question_How-I-unset-variable-array-element-object-property-JavaScript_836.aspx Quote Link to comment Share on other sites More sharing options...
Karlos Posted January 6, 2009 Author Share Posted January 6, 2009 Re: Variables.. I have tried many ways now and yet still no luck. Thanks for having time to try and help :wink: Quote Link to comment Share on other sites More sharing options...
Karlos Posted January 6, 2009 Author Share Posted January 6, 2009 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(); ?> Quote Link to comment Share on other sites More sharing options...
POG1 Posted January 6, 2009 Share Posted January 6, 2009 Re: Variables.. document.write is not exactly the best thing to use but for this purpose it is ok. Quote Link to comment Share on other sites More sharing options...
Floydian Posted January 7, 2009 Share Posted January 7, 2009 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. Quote Link to comment Share on other sites More sharing options...
Karlos Posted January 7, 2009 Author Share Posted January 7, 2009 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(); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.