From 87d3eab4e7188a6a196faf98eae7f01d69ab8329 Mon Sep 17 00:00:00 2001 From: awadbilal Date: Tue, 25 May 2021 01:38:23 +0300 Subject: [PATCH] Bilal Avvad && Isa Tekinkaya class 14 activities. --- .../bilal-isa/diagonal-difference/index.js | 38 ++++++++++++++ .../bilal-isa/diagonal-difference/readme.md | 21 ++++++++ .../bilal-isa/wheres-waldo/index.js | 52 +++++++++++++++++++ .../bilal-isa/wheres-waldo/readme.md | 32 ++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 class-14-js-wheresWaldo/bilal-isa/diagonal-difference/index.js create mode 100644 class-14-js-wheresWaldo/bilal-isa/diagonal-difference/readme.md create mode 100644 class-14-js-wheresWaldo/bilal-isa/wheres-waldo/index.js create mode 100644 class-14-js-wheresWaldo/bilal-isa/wheres-waldo/readme.md diff --git a/class-14-js-wheresWaldo/bilal-isa/diagonal-difference/index.js b/class-14-js-wheresWaldo/bilal-isa/diagonal-difference/index.js new file mode 100644 index 0000000..a202b77 --- /dev/null +++ b/class-14-js-wheresWaldo/bilal-isa/diagonal-difference/index.js @@ -0,0 +1,38 @@ +// Partners: Bilal Avvad && Isa Tekinkaya + +const arr1 = [ + [11, 2, 4], + [4, 5, 6], + [10, 8, -12] +]; // 15 + +const arr2 = [ + [1, 2, 3], + [4, 5, 6], + [9, 8, 9] +]; // 2 + +const arr3 = [ + [1, 2, 3, 4, 5], + [2, 5, 7, 8, 5], + [9, 30, 1, 7, 8], + [7, 5, 4, 2, 5], + [9, 21, 1, 17, 8] +]; // 11 + +const diagonalDifference = (array) => { + let rightSum = 0, leftSum = 0, leftRight = 0, rightLeft = array.length - 1; + for (let i = 0; i < array.length; i++){ + rightSum += array[leftRight][leftRight]; + leftRight++; + + leftSum += array[i][rightLeft]; + rightLeft--; + } + + return Math.abs(rightSum - leftSum); +}; + +console.log(diagonalDifference(arr1)); +console.log(diagonalDifference(arr2)); +console.log(diagonalDifference(arr3)); \ No newline at end of file diff --git a/class-14-js-wheresWaldo/bilal-isa/diagonal-difference/readme.md b/class-14-js-wheresWaldo/bilal-isa/diagonal-difference/readme.md new file mode 100644 index 0000000..490e1d7 --- /dev/null +++ b/class-14-js-wheresWaldo/bilal-isa/diagonal-difference/readme.md @@ -0,0 +1,21 @@ +## Diagonal Difference +Given a square matrix, calculate the absolute difference between the sums of its diagonals. +For example, the square matrix is shown below: + + 1 2 3 + 4 5 6 + 9 8 9 + +- The left-to-right diagonal = `1 + 5 + 9 = 15`. +- The right to left diagonal = `3 + 5 + 9 + 17`. - Their absolute difference is `|15 - 17| = 2`. + + +### Function description +Complete the `diagonalDifference` function in the editor. +- `diagonalDifference` takes two dimensional array of integers as parameter. +- Returns the absolute diagonal difference + +### Example: + [[11, 2, 4], + [4, 5, 6], => 15 + [10, 8, -12]] \ No newline at end of file diff --git a/class-14-js-wheresWaldo/bilal-isa/wheres-waldo/index.js b/class-14-js-wheresWaldo/bilal-isa/wheres-waldo/index.js new file mode 100644 index 0000000..f58bfe8 --- /dev/null +++ b/class-14-js-wheresWaldo/bilal-isa/wheres-waldo/index.js @@ -0,0 +1,52 @@ +// Partners: Bilal Avvad && Isa Tekinkaya + +const arr1 = [ + ["A", "A", "A"], + ["A", "A", "A"], + ["A", "B", "A"] +]; + +const arr2 = [ + ["c", "c", "c", "c"], + ["c", "c", "c", "d"] +]; + +const arr3 = [ + ["O", "O", "O", "O"], + ["O", "O", "O", "O"], + ["O", "O", "O", "O"], + ["O", "O", "O", "O"], + ["P", "O", "O", "O"], + ["O", "O", "O", "O"] +]; + +const whereIsWaldoTest = (array) => { + let location = []; + for (let i = 0; i < array.length; i++){ + for (let j = 0; j < array[i].length; j++){ + if (j === array[i].length - 1 && array[i][j] !== array[i][0]) { + + location.push(i+1); + location.push(j+1); + break; + } + else if (j !== 0){ + if(array[i][j] !== array[i][j+1] && array[i][j] !== array[i][j-1]) { + location.push(i+1); + location.push(j+1); + break; + } + } + else if(array[i][j] !== array[i][j+1] && array[i][j] !== array[i][j+2]) { + location.push(i+1); + location.push(j+1); + break; + } + } + } + return location; +}; + +console.log(whereIsWaldoTest(arr1)); +console.log(whereIsWaldoTest(arr2)); +console.log(whereIsWaldoTest(arr3)); \ No newline at end of file diff --git a/class-14-js-wheresWaldo/bilal-isa/wheres-waldo/readme.md b/class-14-js-wheresWaldo/bilal-isa/wheres-waldo/readme.md new file mode 100644 index 0000000..d62fa87 --- /dev/null +++ b/class-14-js-wheresWaldo/bilal-isa/wheres-waldo/readme.md @@ -0,0 +1,32 @@ +## Where's Waldo? + +Return the coordinates ([row, col]) of the element that differs from the rest. + +### Notes: + +- Rows and columns are 1-indexed (not zero-indexed). +- If you get stuck on a challenge please search it online and try to find resources +- If you are really stuck, please ask your Instructors. + + +### Examples: + + whereIsWaldo([ + ["A", "A", "A"], + ["A", "A", "A"], + ["A", "B", "A"] + ]) ➞ [3, 2] + + whereIsWaldo([ + ["c", "c", "c", "c"], + ["c", "c", "c", "d"] + ]) ➞ [2, 4] + + whereIsWaldo([ + ["O", "O", "O", "O"], + ["O", "O", "O", "O"], + ["O", "O", "O", "O"], + ["O", "O", "O", "O"], + ["P", "O", "O", "O"], + ["O", "O", "O", "O"] + ]) ➞ [5, 1]