...

View Full Version : Resolved I need help with variables



chickentulip
01-16-2012, 06:09 PM
I wrote a script: when the same button is clicked for the first time, it should give a message "Hello World", for the second time "hello Earth". The script is not working.
I don't want anybody to re-write the script, but to look at how i define variables and tell me where I am wrong. I think, this is where the main error is.

my current script just outputs the message "hello world".

my assumption is that functions can alter the value of a global variable.


Thank you very much.





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> title</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<script type="text/javascript">
var count=0;

function changePar() {
if(count===0)
first();


if(count===1)
second();
}


function first() {
document.getElementById("changingParagraph").innerHTML="Hello World";
var count=1;
}

function second() {
document.getElementById("changingParagraph").innerHTML="Hello Earth";
var count=0;
}

</script>

</head>
<body>
<button onclick="changePar()">Click Here</button>
<p id="changingParagraph"></p>
</body>
</html>

Philip M
01-16-2012, 06:33 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> title</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<script type="text/javascript">

var count=0;

function changePar() {

if(count==0) {
first();
return false; // terminate
}

if(count==1) {
second();
return false; // terminate
}

}



function first() {
document.getElementById("changingParagraph").innerHTML="Hello World";
count=1; // no var prefix
}

function second() {
document.getElementById("changingParagraph").innerHTML="Hello Earth";
count=0; // no var prefix
}

</script>

</head>
<body>
<button onclick="changePar()">Click Here</button>
<p id="changingParagraph"></p>
</body>
</html>

a) the var prefix within a function makes the variable local to that function, and hence not available to other functions. Your count variable is global scope, but the value never changes. In your code you have three different variables, all named "count". They do not conflict, as one is global and two are local. But obviously it is a bad idea to give the same name to two different variables.

b) You need to add return false to the changePar() function, as indicated. Otherwise first() runs and immediately changes the value of count to 1, so causing second() to run.

c) It is always a bad idea to leave out the braces surrounding an if or for statement, even if only one line of code follows.

d) == is comparison - equal to. === is exactly equal to (value and type). It is not required here.


BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.




Quizmaster: In The United States a patent was granted on March 7th 1876 to Alexander Graham Bell for which invention?
Contestant: The lightbulb

DaveyErwin
01-16-2012, 06:37 PM
var count=0;// this count is global

function changePar() {
if(count===0)
first();


if(count===1)
second();
}


function first() {
document.getElementById("changingParagraph").innerHTML="Hello World";
var count=1;//because you have used the var keyword this local
count is not the same variable as the global count, they are two
different variables. Leave off the var and they will be the same
variable.
}

chickentulip
01-16-2012, 06:59 PM
Thank you all



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum