-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagicSquares.js
More file actions
63 lines (59 loc) · 1.48 KB
/
magicSquares.js
File metadata and controls
63 lines (59 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function isMagic(input) {
var numbers = [].concat.apply([], input),
max = numbers.length,
side = Math.sqrt(max),
sum = side*(side*side + 1)/2,
add = function(a, b){
return a+b
}, rows = function(){
for (var x=0; x<side; x++) {
var total = 0
for (var y=0; y<side; y++) {
total+=numbers[side*x+y]
}
if (total !== sum) {
console.log('failed on row '+x+'. Expected '+sum+' but got '+total)
return false
}
}
return true
}, columns = function(){
for (var y=0; y<side; y++) {
var total = 0
for (var x=0; x<side; x++) {
total+=numbers[side*x+y]
}
if (total !== sum) {
console.log('failed on column '+y)
return false
}
}
return true
}, diagonalOne = function() {
var total = 0
for (var i=0; i<side; i++) {
total+=numbers[side*i+i]
}
if (total !== sum) {
console.log("failed top-left to bottom-right")
return false
}
return true
}, diagonalTwo = function() {
var total = 0
for (var i=0; i<side; i++) {
total+=numbers[side*i+side-i-1]
}
if (total !== sum) {
console.log("failed top-right to bottom-left")
return false
}
return true
}
if (side % 1 != 0) throw new RangeError('The supplied array was not square.')
for (var i=1; i<max; i++) {
if (!input.indexOf(i) == -1) throw new RangeError('The supplied square did not contain all numbers 1-'+max+'.')
}
if (rows() && columns() && diagonalOne() && diagonalTwo()) return true;
return false;
}