Skip to content
83 changes: 83 additions & 0 deletions starter-code/src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers( num1, num2 ) {
let result = num1 >= num2 ? num1 : num2;
return result;
}

// Iteration #2: Find longest word
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
function findLongestWord(arr) {

//Simple local comprobation
console.log(arr.map( elm => ({[elm]: elm.length}) ))

if ( arr.length > 0) {
return arr.
reduce( (acc, cur) => acc.length >= cur.length ? acc : acc = cur ,
0);
} else {
return null;
}
}

// Iteration #3: Calculate the sum

const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
function sumArray( arr ) {
if (arr.length > 0) {
return arr.
reduce( (acc, cur) => acc + cur );
} else return 0;
}

// Iteration #4: Calculate the average
// Level 1: Array of numbers
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
function averageNumbers( numsArr ) {
if ( numsArr.length > 0 ) {
let sum = numsArr.reduce( (acc, cur) => acc + cur );
let avg = sum / numsArr.length;
return avg;
} else return null;
}

// Level 2: Array of strings
const wordsArr = [
Expand All @@ -24,6 +54,13 @@ const wordsArr = [
'fuel',
'palace'
];
function averageWordLength( wordsArr ) {
if ( wordsArr.length > 0 ) {
let sum = wordsArr.reduce( (acc, cur) => acc + cur.length , 0);
let avg = sum / wordsArr.length;
return avg;
} else return null;
}

// Iteration #5: Unique arrays
const wordsUnique = [
Expand All @@ -39,6 +76,9 @@ const wordsUnique = [
'simple',
'bring'
];
function uniquifyArray( wordsArr ) {
return wordsArr.filter( (elm, idx, arr) => arr.indexOf(elm) == idx );
}

// Iteration #6: Find elements
const wordsFind = [
Expand All @@ -51,6 +91,9 @@ const wordsFind = [
'truth',
'disobedience'
];
function doesWordExist ( wordsArr, wordSearch ) {
return wordsArr.includes(wordSearch)
}

// Iteration #7: Count repetition
const wordsCount = [
Expand All @@ -66,6 +109,9 @@ const wordsCount = [
'disobedience',
'matter'
];
function howManyTimes( wordsArr, wordSearch ) {
return wordsArr.filter( elm => elm === wordSearch).length
}

// Iteration #8: Bonus

Expand All @@ -91,3 +137,40 @@ const matrix = [
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
];
function greatestProduct( matrix, n=4 ) {
let greatest = [];

//Horizontal adjacents
for ( let a = 0; a < matrix.length; a++ ) {
for ( let i = 0; i <= matrix[a].length - n; i++) {
let prod = 1;
for (let j = i; j < i + n; j++) {
prod *= matrix[a][j];
}
greatest.push(prod);
}
}

// Vertical adjacents
for ( let a = 0; a < matrix.length; a++ ) {
for ( let i = 0; i <= matrix.length - n; i++ ) {
let prod = 1;
for ( let j = i; j < i + n; j++ ) {
prod *= matrix[j][a];
}
greatest.push(prod);
}
}

// Diagonal adjacents
for ( let a = 0; a <= matrix.length - n; a++ ) {
for ( let i = 0; i <= matrix[a].length - n; i++ ) {
let prod = 1;
for ( let j = 0; j < n; j++ ) {
prod *= matrix[a + j][i + j];
}
greatest.push(prod);
}
}
return Math.max(...greatest);
}