From 7543ffc033b251b55c32ce83133b789e520995d6 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 20 May 2024 13:26:47 -0400 Subject: Week 270 --- challenge-270/zapwai/javascript/ch-1.js | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 challenge-270/zapwai/javascript/ch-1.js (limited to 'challenge-270/zapwai/javascript/ch-1.js') 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); +} -- cgit