I\'m trying to figure out the Javascript for a project. Here\'s what I\'m doing:
ID: 3546540 • Letter: I
Question
I'm trying to figure out the Javascript for a project. Here's what I'm doing:
I need to use HTML TEXT FIELDS (<input type="text"... >) to capture row and column numbers, in order to "generate a rectangle".
These are to be "generated" using NESTED LOOPS. There will be 3 of these created.
The FIRST one must be generated by NESTED FOR loops
The SECOND one must be generated by NESTED WHILE loops
The THIRD one (and its related calculation) must be generated by NESTED DO...WHILE loops.
The HTML text fields will ask for the two numbers (one for row and one for column, respectively).
A Javascript button under the two areas for numbers will start the calculations.
UNDER THE BUTTON:
After the button is clicked, it will show:
First, the nexted for loop rectangle
Second, the nested while loop rectangle
third, the nested do-while loop rectangle.
FOR EXAMPLE:
If the two numbers are 3 and 5, here's what would show once the numbers were entered:
________________________________________________
The rectangle has 3 rows and 5 columns:
Nested FOR loop rectangle:
FFFFF
FFFFF
FFFFF
Nested WHILE loop rectangle
W,row1,col1;W,row1,col2;W,row1,col3;W,row1,col4;W,row1,col5;
W,row2,col1;W,row2,col2;W,row2,col3;W,row2,col4;W,row2,col5;
W,row3,col1;W,row3,col2;W,row3,col3;W,row3,col4;W,row3,col5;
Nested DO-WHILE loop rectangle
1x1=1; 1x2=2; 1x3=3; 1x4=4; 1x5=5;
2x1=2; 2x2=4; 2x3=6; 2x4=8; 2x5=10;
3x1=3; 3x2=6; 3x3=9; 3x4=12; 3x5=15;
____________________________________________________________
See what I mean about "rectangles" ? I hope this is clear enough. If you need clarification, let me know. I appreciate your help in advance.
Explanation / Answer
Try this :
<!DOCTYPE html>
<html>
<head><script>
var value = "" ;
function print()
{
value = "" ;
document.getElementById("foroutput").innerHTML= " " ;
document.getElementById("whileoutput").innerHTML= " " ;
document.getElementById("dowhileoutput").innerHTML= " " ;
var rows= document.getElementById("rows").value;
var columns = document.getElementById("columns").value;
for (var i=0 ;i<rows ;i++)
{
for (var j=0 ;j<columns ;j++)
{
value = value + " F " + "-----" ;
}
value = value + "<br>" ;
}
document.getElementById("foroutput").innerHTML= value ;
value = "" ;
var k =1;
while(k <= rows)
{
var m=1;
while(m <= columns)
{
value = value +"W,"+"row"+k+",col"+m+";" ;
m++;
}
value = value + "<br>" ;
k++ ;
}
document.getElementById("whileoutput").innerHTML= value ;
value = "" ;
var a =1 ;
do
{
var b= 1 ;
do
{
value = value + "" + a+" * "+b+" = "+ a*b + ";" ;
b++ ;
}
while(columns >= b);
value = value + "<br>" ;
a++ ;
}
while(rows >= a);
document.getElementById("dowhileoutput").innerHTML= value ;
}
</script>
</head>
<body>
<form>
<p align="center"> Rows : <input id="rows" type="text" > </p>
<p align="center">Columns:<input id="columns" type="text" > </p>
<p align="center"> <button type="button">Display</button></p>
</form>
<h4>
<div align="center">
<p id="foroutput">for output</p>
<p id="whileoutput">While output</p>
<p id="dowhileoutput">While output</p>
</div>
</h4>
</body>
</html>