Write a java script program that asks the user for Po’s row and column numbers for generating magic rectangles.
o Generate the magic rectangles shown using nested loops (two-dimensional loops). o The first rectangle must be generated by nested while loops. o The second must be generated rectangle by nested do…while loops.
o The third rectangle and related calculation must be generated by nested for loops.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Kung Fu Panda Po Magic Rectangle</title>
<link href="style.css" rel="stylesheet" type="text/css">
<link href="http://ift.tt/2oKNpkN" rel="stylesheet">
<link href="http://ift.tt/2p5pzUe" rel="stylesheet">
</head>
<body>
<h1> Kung Fu Panda Po Magic Rectangle</h1>
<h3>Enter Po's row and column numbers.</h3>
<br>
Po's row: <input type="text" name="fname" id="row"><br><br>
Po's column: <input type="text" name="lname" id="col"><br>
<br><br>
<button onclick="myfun()">generate Po's magic rectangle </button>
<br><br>
<h3>Po's Magic Rectangle:</h3>
<h3 id="demo"></h3>
<div id="draw">
</div>
<div id="draw2">
</div>
<h3 id="demo2"></h3>
<script>
var m = 1,
n = 1,
div = document.getElementById('draw');
function myfun() {
var row1 = document.getElementById("row").value;
var col1 = document.getElementById("col").value;
function writeToDiv(stringToWrite) {
div.innerHTML = div.innerHTML + stringToWrite;
}
//nested while loops
while (m <= row1)
{
while (n <= col1) {
document.getElementById("demo").innerHTML="Po's magig rectangle has " + m + " rows, and "+n+ " columns.<br><br>" +
"Nested while loop rectangle";
writeToDiv("(XY)" + " ");
n++;
}
n=1;
writeToDiv("<br>");
m++;
}
//nested do…while
/* do
{
do {
document.getElementById("demo").innerHTML="<br>Nested do..while loop rectangle";
writeToDiv("(XY)" + " ");
n++;
}while(n <= col1);
n=1;
writeToDiv("<br>");
m++;
}while(m <= row1);*/
}
</script>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire