-
Notifications
You must be signed in to change notification settings - Fork 0
/
magicSquare.js
92 lines (78 loc) · 2.04 KB
/
magicSquare.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// magicSquare.js
const s = [ [ 4, 5, 8 ], [ 2, 4, 1 ], [ 1, 9, 7 ] ]
function formingMagicSquare(s) {
// Complete this function
const sums = findSums(s)
const rowSums = sums[0]
const columnSums = sums[1]
let counter = 0
function bumpUp() {
let lowestRowIndex = findLowestIndex(rowSums)
let lowestColumnIndex = findLowestIndex(columnSums)
columnSums[lowestColumnIndex]++
rowSums[lowestRowIndex]++
counter++
console.log(columnSums, rowSums)
}
function bumpDown() {
let highestRowIndex = findHighestIndex(rowSums)
let highestColumnIndex = findHighestIndex(columnSums)
columnSums[highestColumnIndex]--
rowSums[highestRowIndex]--
counter++
console.log(columnSums, rowSums)
}
console.log(columnSums, rowSums)
while (!rowSums.every(equal15) && !columnSums.every(equal15)){
if (rowSums.find((el) => el < 15)) {
bumpUp()
}
if (rowSums.find((el) => el > 15)) {
bumpDown()
}
}
return counter
}
function equal15(el) {
return el == 15
}
function findLowestIndex(ar) {
let lowest = 1000
let lowestIndex = 0
for (let i=0; i < ar.length; i++) {
if (ar[i] < lowest) {
lowest = ar[i]
lowestIndex = i
}
}
return lowestIndex
}
function findHighestIndex(ar) {
let highest = 0
let highestIndex = 0
for (let i=0; i < ar.length; i++) {
if (ar[i] > highest) {
highest = ar[i]
highestIndex = i
}
}
return highestIndex
}
function findSums(s) {
let sums = []
let rowSums = s.map((row) => {
return row.reduce((el, next) => {
return el + next
})
})
let columnSums = [0, 0, 0];
for (i=0; i < s.length; i++) {
columnSums[i]
+= s[0][i]
+= s[1][i]
+= s[2][i]
}
sums.push(rowSums, columnSums)
return sums
}
console.log(formingMagicSquare(s))