Skip to content
Open
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
125 changes: 96 additions & 29 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,74 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}


function maxOfTwoNumbers() {
return a > b ? a : b;
}

// Iteration #2: Find longest word
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];

function findLongestWord() {}


function findLongestWord(words) {
if (words.length === 0) return null;
let longestWord = words[0];
for (let word of words) {
if (word.length > longestWord.length) {
longestWord = word;
}
}
return longestWord;
}

// Iteration #3: Calculate the sum
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

function sumNumbers() {}


function sumNumbers() {
return numbers.reduce((acc, curr) => acc + curr, 0);
}

// Iteration #3.1 Bonus:
function sum() {}


function sum(mixedArray) {
return mixedArray.reduce((acc, curr) => {
if (typeof curr === 'number') {
return acc + curr;
} else if (typeof curr === 'string') {
return acc + curr.length;
} else {
return acc; // ignore other types
}
}, 0);
}

// Iteration #4: Calculate the average
// Level 1: Array of numbers
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];

function averageNumbers() {}

function averageNumbers(numbers) {
if (numbers.length === 0) return null;
return sumNumbers(numbers) / numbers.length;
}

// Level 2: Array of strings
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];

function averageWordLength() { }
function averageWordLength(wordsArr) {
if (wordsArr.length === 0) return null;
const totalLength = wordsArr.reduce((acc, word) => acc + word.length, 0);
return totalLength / wordsArr.length;
}

// Bonus - Iteration #4.1
function avg() {}
function avg(arr) {
if (arr.length === 0) return null;
const total = arr.reduce((acc, item) => {
if (typeof item === 'number') {
return acc + item;
} else if (typeof item === 'string') {
return acc + item.length;
}
return acc;
}, 0);

return total / arr.length;
}

// Iteration #5: Unique arrays
const wordsUnique = [
Expand All @@ -52,16 +85,16 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}


function uniquifyArray(wordsUnique) {
return [...new Set(wordsUnique)];
}

// Iteration #6: Find elements
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];

function doesWordExist() {}


function doesWordExist(wordsFind, word) {
return wordsFind.includes(word);
}

// Iteration #7: Count repetition
const wordsCount = [
Expand All @@ -78,9 +111,9 @@ const wordsCount = [
'matter'
];

function howManyTimes() {}


function howManyTimes(wordsCount, word) {
return wordsCount.filter((w) => w === word).length;
}

// Iteration #8: Bonus
const matrix = [
Expand All @@ -106,10 +139,44 @@ const matrix = [
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
];

function greatestProduct() {}



function greatestProduct(matrix) {
let maxProduct = 0;

const rows = matrix.length;
const cols = matrix[0].length;

for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
// Check right (horizontal)
if (j < cols - 3) {
const horizontalProduct = matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3];
maxProduct = Math.max(maxProduct, horizontalProduct);
}

// Check down (vertical)
if (i < rows - 3) {
const verticalProduct = matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j];
maxProduct = Math.max(maxProduct, verticalProduct);
}

// Check diagonal down-right
if (i < rows - 3 && j < cols - 3) {
const diagonalDownRightProduct =
matrix[i][j] * matrix[i + 1][j + 1] * matrix[i + 2][j + 2] * matrix[i + 3][j + 3];
maxProduct = Math.max(maxProduct, diagonalDownRightProduct);
}

// Check diagonal down-left
if (i < rows - 3 && j > 2) {
const diagonalDownLeftProduct =
matrix[i][j] * matrix[i + 1][j - 1] * matrix[i + 2][j - 2] * matrix[i + 3][j - 3];
maxProduct = Math.max(maxProduct, diagonalDownLeftProduct);
}
}
}

return maxProduct;
}

// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */
Expand Down