aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNuno Vieira <nunovieira220@gmail.com>2020-11-27 00:33:00 +0000
committerNuno Vieira <nunovieira220@gmail.com>2020-11-27 00:33:00 +0000
commitad23c66c6119e7cc50dcb9d64a3f07ef9f8bda1f (patch)
tree740cbbaac45f1e5970453f3e14179b2bb9168fbb
parentb8089c321ba367660b76992e2a1fbc2c798baefc (diff)
downloadperlweeklychallenge-club-ad23c66c6119e7cc50dcb9d64a3f07ef9f8bda1f.tar.gz
perlweeklychallenge-club-ad23c66c6119e7cc50dcb9d64a3f07ef9f8bda1f.tar.bz2
perlweeklychallenge-club-ad23c66c6119e7cc50dcb9d64a3f07ef9f8bda1f.zip
Add nunovieira220 js solution to challenge 088
-rw-r--r--challenge-088/nunovieira220/js/ch-1.js13
-rw-r--r--challenge-088/nunovieira220/js/ch-2.js40
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-088/nunovieira220/js/ch-1.js b/challenge-088/nunovieira220/js/ch-1.js
new file mode 100644
index 0000000000..feb58d7db2
--- /dev/null
+++ b/challenge-088/nunovieira220/js/ch-1.js
@@ -0,0 +1,13 @@
+// Input
+const N = [2, 1, 4, 3];
+
+// Array of Product
+const res = [];
+
+for(let i = 0; i < N.length; i++) {
+ const aux = N.filter((_, index) => i !== index);
+ res.push(aux.reduce((a, b) => a * b));
+}
+
+// Output
+console.log(res); \ No newline at end of file
diff --git a/challenge-088/nunovieira220/js/ch-2.js b/challenge-088/nunovieira220/js/ch-2.js
new file mode 100644
index 0000000000..87c256507f
--- /dev/null
+++ b/challenge-088/nunovieira220/js/ch-2.js
@@ -0,0 +1,40 @@
+// Handle spiral matrix
+const handleMatrix = (matrix) => {
+ let res = [];
+
+ if(!matrix.length) return res;
+
+ const firstRow = matrix.splice(0, 1)[0];
+ res = res.concat(firstRow);
+
+ if(matrix.length > 0) {
+ for(let i = 0; i < matrix.length - 1; i++) {
+ const lastElem = matrix[i].splice(matrix[i].length - 1, 1)[0];
+ res.push(lastElem);
+ }
+
+ const lastRow = matrix.splice(matrix.length - 1, 1)[0];
+ res = res.concat(lastRow.reverse());
+
+ for(let i = matrix.length - 1; i >= 0; i--) {
+ const firstElem = matrix[i].splice(0, 1)[0];
+ res.push(firstElem);
+ }
+ }
+
+ return res.concat(handleMatrix(matrix));
+}
+
+// Input
+const N = [
+ [ 1, 2, 3, 4],
+ [ 5, 6, 7, 8],
+ [ 9, 10, 11, 12],
+ [13, 14, 15, 16]
+];
+
+// Spiral Matrix
+const res = handleMatrix(N);
+
+// Output
+console.log(res); \ No newline at end of file