← Back to challenges

Magic Square

JavaScriptHardarraysinterviewloopsmath

Instructions

In recreational mathematics, a square array of numbers, usually positive integers, is called a magic square if the sums of the numbers in each row, each column, and both main diagonals are the same.

Create a function that takes a square 2D array as an argument and returns a Boolean (if it is: true, if it isn't: false).

Examples

isMagicSquare([
  [2, 7, 6],
  [9, 5, 1],
  [4, 3, 8]
]) ➞ true

isMagicSquare([
  [16, 3, 2, 13],
  [5, 10, 11, 8],
  [9, 6, 7, 12],
  [4, 15, 14, 1]
]) ➞ true

isMagicSquare([
  [1, 14, 14, 4],
  [11, 7, 6, 9],
  [8, 11, 10, 5],
  [13, 2, 3, 15]
]) ➞ false

Notes

N/A

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.