Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bilal-isa-class14-whereswaldo #170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions class-14-js-wheresWaldo/bilal-isa/diagonal-difference/index.js
Original file line number Diff line number Diff line change
@@ -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));
21 changes: 21 additions & 0 deletions class-14-js-wheresWaldo/bilal-isa/diagonal-difference/readme.md
Original file line number Diff line number Diff line change
@@ -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]]
52 changes: 52 additions & 0 deletions class-14-js-wheresWaldo/bilal-isa/wheres-waldo/index.js
Original file line number Diff line number Diff line change
@@ -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));
32 changes: 32 additions & 0 deletions class-14-js-wheresWaldo/bilal-isa/wheres-waldo/readme.md
Original file line number Diff line number Diff line change
@@ -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]