1

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>&nbsp;</td>
<td><span id="cell01"></span>&nbsp;</td>
<td><span id="cell02"></span>&nbsp;</td>
</tr><tr>
<td><span id="cell10"></span>&nbsp;</td>
<td><span id="cell11"></span>&nbsp;</td>
<td><span id="cell12"></span>&nbsp;</td>
</tr><tr>
<td><span id="cell20"></span>&nbsp;</td>
<td><span id="cell21"></span>&nbsp;</td>
<td><span id="cell22"></span>&nbsp;</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">&nbsp;</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");

}

7
  • Please use the code editor or create a fiddle Commented Apr 19, 2015 at 10:03
  • 1
    Why do you sometimes use myArray[i][j].innerHTML and sometimes just myArray[i][j] ? Commented Apr 19, 2015 at 10:05
  • (can't really help without seeing how you created myArray) Commented Apr 19, 2015 at 10:05
  • You have some syntax errors on innerHTML Commented Apr 19, 2015 at 10:12
  • rowSum is the sum of everything, it's not just one row. And so is colSum. Commented Apr 19, 2015 at 10:14

1 Answer 1

1

Alright… your array contains table cells, so you should always read their value with myArray[i][j].textContent (or innerHTML if you prefer). That'll fix one problem.

Next, their value is a string. No point in trying to convert when you get the input, because text inside the DOM is always strings. So when you read, you get a string, and that changes the semantics of + to concatenation instead of addition. So actually you should read with +myArray[i][j].textContent (using + as a unary operator for converting to Number).

Finally, as Barmar pointed out, you should check each row and column separately, not sum them all up (i.e. initialize sum to zero on each iteration of the first loop, and just before the iteration ends, compare with the diagonal sum).

Pretty messy and inefficient code too, but we're not on CodeReview…

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.