Write a program that allows the user to enter Po’s magic numbers. Po’s magic squ
ID: 3709508 • Letter: W
Question
Write a program that allows the user to enter Po’s magic numbers.
Po’s magic square is a two-dimensional array of positive integers in which the following is true:
? The number of rows equals the number of columns.
? Every row, column, and the two diagonals add up to the same number.
o The web page must have a table that has three rows and three columns.
o Allow the user to enter values for each cell.
o Store the values in a two-dimensional array named RichardMagic and determine if it is Po’s magic square.
Even if all the rows, colums, and diagnols add up to the same number my program will continue to give a wrong answer. Help please!
<!DOCTYPE html>
<html lang="en">
<head>
<title>Kung Fu Panda Po Magic Square</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script>
var RichardMagic = new Array(3)
for (var i=0; i <3; i++){
RichardMagic[i] = new Array(3)}
function enterNumbers(){
for (var row=0; row<3; row++){
for(var col=0; col<3 ; col++){
var rowNumber = row+1; var colNumber = col+1;
RichardMagic[row][col] = prompt("Enter a number for row "+rowNumber+", column "+colNumber+":", "");
var ID = "r"+rowNumber+"c"+colNumber;
document.getElementById(ID).innerHTML = RichardMagic[row][col];}}}
function checkForMagicSquare(){
var magic = false;
var sum = 0;
for (var col=0; col<3; col++){
sum = sum + RichardMagic[0][col];}
var rowSum = 0;
for (var i=1; i<3; i++){
for (var j=0; j<3; j++){
rowSum = rowSum + RichardMagic[i][j];
if (rowSum != sum){
magic = false;
break;}}}
var sum_diagonal = 0;
if (magic){
for (var i=0; i<3; i++)
for (var j=0; j<3; j++)
if (i==j)
sum_diagonal += RichardMagic[i][j];
if (sum_diagonal != sum){
magic = false;}}
if (magic){
sum_diagonal = 0;
for (var i=0; i<3; i++)
for (var j=0; j<3; j++)
if ((i+j) == 2)
sum_diagonal += RichardMagic[i][j];
if (sum_diagonal != sum){
magic = false;}}
if (magic == true){document.getElementById("result").innerHTML = "Wow! This is a magic square";}
else {document.getElementById("result").innerHTML = "Sorry, your square is not a magic square";}}
</script>
</head>
<body>
<header>
<h1>Kung Fu Panda Po Magic Square.</h1>
</header>
<h2>Richard will check if the numbers form a magic square.
<br/>A magic square exists when all the rows, columns and diagonals of a square add up to the same number.
</h2>
<br/><input type="submit" id="numsButton" value="Enter Po's magic numbers">
<br/><br/>
<table border='1'>
<tbody>
<tr><td id="r1c1"></td><td id="r1c2"></td><td id="r1c3"></td></tr>
<tr><td id="r2c1"></td><td id="r2c2"></td><td id="r2c3"></td></tr>
<tr><td id="r3c1"></td><td id="r3c2"></td><td id="r3c3"></td></tr>
<tr><td colspan='3'><input type="submit" id="checkButton" value="Check if it a magic number"></td></tr>
<tr><td>Result:</td><td colspan='2' id="result"></td></tr>
</tbody>
</table>
</body>
</html>
Richard Ricardo's Kung Fu Panda Po Magic Square Richard will check if the numbers form a magic square. A magic square exists when all the rows, columns, and diagonals of a square add up to the same number. Enter Po's magic numbers 2 3 4 5 6 7 8 9 Check if it is a magic square Sorry, your square is not a magic square Result:Explanation / Answer
I've provided comments in the javascript code where the modifications happend.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Kung Fu Panda Po Magic Square</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script>
var RichardMagic = new Array(3)
for (var i=0; i <3; i++){
RichardMagic[i] = new Array(3)}
function enterNumbers(){
for (var row=0; row<3; row++){
for(var col=0; col<3 ; col++){
var rowNumber = row+1; var colNumber = col+1;
RichardMagic[row][col] = prompt("Enter a number for row "+rowNumber+", column "+colNumber+":", "");
var ID = "r"+rowNumber+"c"+colNumber;
document.getElementById(ID).innerHTML = RichardMagic[row][col];}}}
function checkForMagicSquare(){
var magic = true;
var sum = 0;
for (var col=0; col<3; col++){
sum = sum + Number(RichardMagic[0][col]);//Here we need to convert array element into number, so use Number() for that purpose
}
var rowSum = 0;
for (var i=1; i<3; i++)
{
rowSum=0;
for (var j=0; j<3; j++)
{
rowSum = rowSum + Number(RichardMagic[i][j]);//Here we need to convert array element into number, so use Number() for that purpose
}
//After calculating the sum of each row only we need to compare
if (rowSum != sum)
{
magic = false;
break;
}
}
var sum_diagonal = 0;
if (magic){
for (var i=0; i<3; i++)
for (var j=0; j<3; j++)
if (i==j)
sum_diagonal += Number(RichardMagic[i][j]);//Here we need to convert array element into number, so use Number() for that purpose
}
//After calculating the entire diagonal sum only we need to compare
if (sum_diagonal != sum)
{
magic = false;
}
if (magic){
sum_diagonal = 0;
for (var i=0; i<3; i++)
for (var j=0; j<3; j++)
if ((i+j) == 2)
sum_diagonal += Number(RichardMagic[i][j]);//Here we need to convert array element into number, so use Number() for that purpose
}
//After calculating the entire diagonal sum only we need to compare
if (sum_diagonal != sum)
{
magic = false;
}
if (magic == true)
{
document.getElementById("result").innerHTML = "Wow! This is a magic square";
}
else
{
document.getElementById("result").innerHTML = "Sorry, your square is not a magic square";
}
}
</script>
</head>
<body>
<span id="x1"></span>
<span id="x2"></span>
<span id="x3"></span>
<span id="x4"></span>
<span id="x5"></span>
<span id="x6"></span>
<span id="x7"></span>
<header>
<h1>Kung Fu Panda Po Magic Square.</h1>
</header>
<h2>Richard will check if the numbers form a magic square.
<br/>A magic square exists when all the rows, columns and diagonals of a square add up to the same number.
</h2>
<br/><input type="submit" id="numsButton" value="Enter Po's magic numbers">
<br/><br/>
<table border='1'>
<tbody>
<tr><td id="r1c1"></td><td id="r1c2"></td><td id="r1c3"></td></tr>
<tr><td id="r2c1"></td><td id="r2c2"></td><td id="r2c3"></td></tr>
<tr><td id="r3c1"></td><td id="r3c2"></td><td id="r3c3"></td></tr>
<tr><td colspan='3'><input type="submit" id="checkButton" value="Check if it a magic number"></td></tr>
<tr><td>Result:</td><td colspan='2' id="result"></td></tr>
</tbody>
</table>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.