aboutsummaryrefslogtreecommitdiff
path: root/challenge-270/zapwai/javascript/ch-1.js
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-270/zapwai/javascript/ch-1.js')
-rw-r--r--challenge-270/zapwai/javascript/ch-1.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-270/zapwai/javascript/ch-1.js b/challenge-270/zapwai/javascript/ch-1.js
new file mode 100644
index 0000000000..d8b130e799
--- /dev/null
+++ b/challenge-270/zapwai/javascript/ch-1.js
@@ -0,0 +1,50 @@
+let matrix = [ [1, 0, 0],
+ [0, 0, 1],
+ [1, 0, 0],
+ ];
+
+let matrix2 = [ [1, 0, 0],
+ [0, 1, 0],
+ [0, 0, 1],
+ ];
+
+proc(matrix);
+proc(matrix2);
+
+function is_special(m, M, N, i, j) {
+ if (m[i][j] != 1) {
+ return 0;
+ }
+ for (let k = 0; k < M; k++) {
+ if (k == i) {
+ continue;
+ }
+ if (m[k][j] != 0) {
+ return 0;
+ }
+ }
+ for (let k = 0; k < N; k++) {
+ if (k == j) {
+ continue;
+ }
+ if (m[i][k] != 0) {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+function proc(m) {
+ let M = m.length;
+ let N = m[0].length;
+ console.log("Input: m = ", m);
+ let cnt = 0;
+ for (let i = 0; i < M; i++) {
+ for (let j = 0; j < N; j++) {
+ if (is_special(m, M, N, i, j)) {
+ cnt++;
+ }
+ }
+ }
+ console.log("Output: ", cnt);
+}