PDA

View Full Version : Getting a loop to repeat by resetting variable



evan
04-23-2009, 05:37 PM
this code will only execute twice.
what I want it to do is reset the value and start the loop over again
I don't know why it only works twice.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/stict.dtd">
<html>
<head>

<title>do while experiment</title>

</head>
<body>
you have 6 shots


<script type="text/javascript">

var load = 0;
bang();
function bang()
{

do {alert("you have " + load +" shots fired allready");

load++;

}

while (load <7);
}

if
(load = 7)
{

alert("you are out of shots-click to reload");
load = 0;
bang();

}


</script>


</body>

jscheuer1
04-23-2009, 07:38 PM
I think your function ends before you intend it to (some blank lines removed for clarity, end comment added):


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/stict.dtd">
<html>
<head>
<title>do while experiment</title>
</head>
<body>
you have 6 shots
<script type="text/javascript">
var load = 0;
bang();
function bang()
{
do {alert("you have " + load +" shots fired allready");
load++;
}
while (load <7);
} //bang() ENDS Here
if
(load = 7)
{
alert("you are out of shots-click to reload");
load = 0;
bang();
}
</script>
</body>

If you want it to be continuous:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/stict.dtd">
<html>
<head>
<title>do while experiment</title>
</head>
<body>
you have 6 shots
<script type="text/javascript">
var load = 0;
bang();
function bang()
{
do {alert("you have " + load +" shots fired allready");
load++;
}
while (load <7);
alert("you are out of shots-click to reload");
load = 0;
bang();
}
</script>
</body>

But I wouldn't recommend it, it will be an endless loop.