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
83 changes: 71 additions & 12 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,65 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(a, b) {
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;
return words.reduce((longest, word) => word.length > longest.length ? word : longest, words[0]);
}



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

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



// Iteration #3.1 Bonus:
function sum() {}
function sum(arr) {
return arr.reduce((total, item) => {
if (typeof item === 'number') return total + item;
if (typeof item === 'string') return total + item.length;
if (typeof item === 'boolean') return total + (item ? 1 : 0);
return total;
}, 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(numbersAvg) {
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 (words.length === 0) return null;
return sum(words) / words.length;
}


// Bonus - Iteration #4.1
function avg() {}
function avg(arr) {
if (arr.length === 0) return null;
return sum(arr) / arr.length;
}

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

function uniquifyArray() {}
function uniquifyArray(wordsUnique) {
return words.length === 0 ? null : [...new Set(words)];
}



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

function doesWordExist() {}
function doesWordExist(wordsFind, word) {
return wordsFind.length === 0 ? null : wordsFind.includes(word);
}



Expand All @@ -78,7 +106,9 @@ const wordsCount = [
'matter'
];

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



Expand Down Expand Up @@ -106,9 +136,38 @@ 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;

for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (j < matrix[i].length - 3) {
let horizontal = matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3];
maxProduct = Math.max(maxProduct, horizontal);
}
if (i < matrix.length - 3) {
let vertical = matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j];
maxProduct = Math.max(maxProduct, vertical);
}
}
}
return maxProduct;
}


// Bonus - Iteration #8.1: Product of diagonals
function greatestProductOfDiagonals(matrix) {
let maxProduct = 0;

for (let i = 0; i < matrix.length - 3; i++) {
for (let j = 0; j < matrix[i].length - 3; j++) {
let diag1 = matrix[i][j] * matrix[i + 1][j + 1] * matrix[i + 2][j + 2] * matrix[i + 3][j + 3];
let diag2 = matrix[i][j + 3] * matrix[i + 1][j + 2] * matrix[i + 2][j + 1] * matrix[i + 3][j];
maxProduct = Math.max(maxProduct, diag1, diag2);
}
}
return maxProduct;
}


// The following is required to make unit tests work.
Expand Down