From ad23c66c6119e7cc50dcb9d64a3f07ef9f8bda1f Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Fri, 27 Nov 2020 00:33:00 +0000 Subject: Add nunovieira220 js solution to challenge 088 --- challenge-088/nunovieira220/js/ch-1.js | 13 +++++++++++ challenge-088/nunovieira220/js/ch-2.js | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 challenge-088/nunovieira220/js/ch-1.js create mode 100644 challenge-088/nunovieira220/js/ch-2.js 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 -- cgit