Thought I was doing well with this but when checking values other than 1's, 2's etc. the output returns false when I know it should be returning true. There are numerous 'magic square' questions here in SO but i'm on my own learning and many of the answers to other questions are too advanced for me as of yet.
Tried several ways to code this 3x3 table I've created have been unsuccessful to find a solution. If it is something simple please excuse my beginner questions.
Revised my code however TRUE is returned when I know that it should be false. I've reset the sums back to zero as the loops iterate through which should be comparing the sums of individual rows and cols with each other and the diagonals. Hopefully someone can point me in the right direction.
updated Code:
<p><input type="button" onClick="simpleSquare()" value="Enter a Positive
Number"></p>
<!--create table here-->
<div id="squareTable">
<table id="magicSquare">
<tr>
<td><span id="cell00"></span> </td>
<td><span id="cell01"></span> </td>
<td><span id="cell02"></span> </td>
</tr><tr>
<td><span id="cell10"></span> </td>
<td><span id="cell11"></span> </td>
<td><span id="cell12"></span> </td>
</tr><tr>
<td><span id="cell20"></span> </td>
<td><span id="cell21"></span> </td>
<td><span id="cell22"></span> </td>
</tr><tr>
<td colspan="3" id="button"><p><input type="button"
onclick="checkMagic()" value="Check if it is a magic Square"></p></td>
</tr><tr>
<td id="output">Result:</td><td colspan="2" id="magic"> </td></tr>
</table></div>
function simpleSquare() {
myArray = new Array([document.getElementById("cell00"), document.getElementById("cell01"),
document.getElementById("cell02")], [document.getElementById("cell10"), document.
getElementById("cell11"), document.getElementById("cell12")],[document.getElementById("cell20"
), document.getElementById("cell21"), document.getElementById("cell22")]);
for(var rows = 0; rows < 3; rows++)
{
for(var cols = 0; cols < 3; cols++)
{
var nums = parseInt(prompt("Enter a number for row " + (rows + 1) + ", column " + (cols + 1) + "."));
myArray[rows][cols].innerHTML = nums;
}
}
}
function checkMagic() {
var flag = true;
var flag = true;
var rowSum = 0;
var colSum = 0;
//get sum of diagonals in magic square
var sumDiag1 = parseInt(myArray[0][0].innerHTML) + parseInt(myArray[1]
[1].innerHTML) + parseInt(myArray[2][2].innerHTML);
var sumDiag2 = parseInt(myArray[2][0].innerHTML) + parseInt(myArray[1]
[1].innerHTML) + parseInt(myArray[0][2].innerHTML);
if(sumDiag1 != sumDiag2)
flag = false;
//sum of rows using for loop
for(j = 0; j < 3; j++)
{
for(i = 0; i < 3; i++)
{
rowSum = rowSum + myArray[i][j];
if((rowSum != sumDiag1) || (rowSum != colSum))
flag == false;
}
rowSum = 0; //reset rowsum before each iteration
}
//sum of columns
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
colSum = colSum + myArray[i][j];
if((colSum != sumDiag1) || (colSum != rowSum))
flag == false;
}
colSum = 0; //reset colsum before each iteration
flag == false;
}
//output to user if magic square
if(flag == false)
document.getElementById("magic").innerHTML=("Sorry, your square is
not a magic square");
if(flag == true)
document.getElementById("magic").innerHTML=("Wow! This is a magic
square");
}
innerHTMLrowSumis the sum of everything, it's not just one row. And so iscolSum.