I\'m to write an HTML/Javascript where : Allow the user to click on one of four
ID: 3557423 • Letter: I
Question
I'm to write an HTML/Javascript where
:
Allow the user to click on one of four buttons (in Javascript) that appear on the page. These buttons will affect the displayed number as follows:
The first button - Use a "FOR loop" to count down by 1
The second button - Use a "WHILE loop" to count down by 2
The third button - Use a "FOR loop" to count up by 1
The fourth button - Use a "WHILE" loop to count up by 2
We'll assume that the count starts at ZERO, of course.
This should be written in Javascript, of course.
Explanation / Answer
<html>
<head>
<script>
function func(n)
{
var s=document.form1.t1.value;
switch(n)
{
case 1:
for(var i=0;i<1;i++)
{
var s=--document.form1.t1.value;
document.form1.t1.value=s;
}
break;
case 2:
var i=0;
while(i<2)
{
i++;
var s=--document.form1.t1.value;
document.form1.t1.value=s;
}
break;
case 3:
for(var i=0;i<1;i++)
{
var s=++document.form1.t1.value;
document.form1.t1.value=s;
}
break;
case 4:
var i=0;
while(i<2)
{
i++;
var s=++document.form1.t1.value;
document.form1.t1.value=s;
}
break;
}
}
</script>
</head>
<body>
<form name="form1">
<input type="Button" value="Button 1" />
<input type="Button" value="Button 2" />
<input type="Button" value="Button 3" />
<input type="Button" value="Button 4" />
<input type="text" id="t1" value=0 />
</form>
</body>
</html>