/
ch-2.js
executable file
·52 lines (48 loc) · 1.35 KB
/
ch-2.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
#! /usr/bin/node
"use strict"
// by Frank Tan
// https://stackoverflow.com/questions/38400594/javascript-deep-comparison
function deepEqual(a,b)
{
if( (typeof a == 'object' && a != null) &&
(typeof b == 'object' && b != null) )
{
var count = [0,0];
for( var key in a) count[0]++;
for( var key in b) count[1]++;
if( count[0]-count[1] != 0) {return false;}
for( var key in a)
{
if(!(key in b) || !deepEqual(a[key],b[key])) {return false;}
}
for( var key in b)
{
if(!(key in a) || !deepEqual(b[key],a[key])) {return false;}
}
return true;
}
else
{
return a === b;
}
}
function weakestrows(a) {
let p = Array(a.length).fill().map((element, index) => index);
let b = a.map(n => n.reduce((x,y) => x+y));
p.sort(function(l, r) {
return b[l] - b[r];
});
return p;
}
if (deepEqual(weakestrows([[1, 1, 0, 0, 0], [1, 1, 1, 1, 0], [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 1, 1, 1]]), [2, 0, 3, 1, 4])) {
process.stdout.write("Pass");
} else {
process.stdout.write("FAIL");
}
process.stdout.write(" ");
if (deepEqual(weakestrows([[1, 0, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0]]), [0, 2, 3, 1])) {
process.stdout.write("Pass");
} else {
process.stdout.write("FAIL");
}
process.stdout.write("\n");