From a52be42edce623312cf64026bdf61ecf2960bbe2 Mon Sep 17 00:00:00 2001 From: mk3346 Date: Thu, 11 May 2023 17:58:55 +0200 Subject: [PATCH 1/2] taking a break --- src/functions-and-arrays.js | 77 ++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 9 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..335d189b1 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,19 +1,41 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(num1, num2) { + return Math.max(num1, num2); +} +const maximum = maxOfTwoNumbers(10, 20); +console.log(maximum); // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} - +function findLongestWord(anyArray) { + if (anyArray.length === 0) { + return null; + } + + let longestWord = anyArray[0]; + for (let i = 1; i < anyArray.length; i++) { + if (anyArray[i].length > longestWord.length) { + longestWord = anyArray[i]; + } + } + return longestWord; +} // Iteration #3: Calculate the sum + const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(sumArray) { + let sum = 0; + for (let i = 0; i < sumArray.length; i++) { + sum += sumArray[i]; + } + return sum; +} @@ -26,16 +48,37 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(arrayAvg) { + if (arrayAvg.length === 0) { + return null; + } + let sum = 0; + for (let i = 0; i < arrayAvg.length; i++) { + sum += arrayAvg[i]; + } + return sum / arrayAvg.length; +} // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(wordAvg) { + if (wordAvg.length === 0) { + return null; + } + let sum = 0; + for (let i = 0; i < wordAvg.length; i++) { + sum += wordAvg[i].length; + } + return sum / wordAvg.length; +} // Bonus - Iteration #4.1 -function avg() {} + +function avg() { + +} // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,14 +95,30 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(words) { + if (words.length === 0) { + return null; + } + let deduplicatedArray = []; + for (let i = 0; i < words.length; i++) { + if (deduplicatedArray.indexOf(words[i]) === -1) { + deduplicatedArray.push(words[i]); + } + } + return deduplicatedArray; +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(wordArr, wordFind) { + if (wordArr === 0){ + return null; + } +return wordArr.included(wordFind); +} From bcf0996f7563ce596e7907cfe30d0be508a6b90c Mon Sep 17 00:00:00 2001 From: mk3346 Date: Thu, 11 May 2023 21:34:59 +0200 Subject: [PATCH 2/2] done --- src/functions-and-arrays.js | 50 +++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 335d189b1..3791111d4 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -3,9 +3,6 @@ function maxOfTwoNumbers(num1, num2) { return Math.max(num1, num2); } -const maximum = maxOfTwoNumbers(10, 20); -console.log(maximum); - // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; @@ -137,8 +134,16 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(inputArray, countWord) { + let sum = 0; + for (let i = 0; i < inputArray.length; i++) { + if (inputArray[i] === countWord) { + sum += 1; + } + } + return sum; +} // Iteration #8: Bonus @@ -165,7 +170,42 @@ 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(bigArray) { + let maxProduct = 0; + + // Find the greatest product of four adjacent numbers horizontally + for (let i = 0; i < bigArray.length; i++) { + for (let j = 0; j < bigArray[i].length - 3; j++) { + let product = + bigArray[i][j] * + bigArray[i][j + 1] * + bigArray[i][j + 2] * + bigArray[i][j + 3]; + if (product > maxProduct) { + maxProduct = product; + } + } + } + + // Find the greatest product of four adjacent numbers vertically + for (let i = 0; i < bigArray.length - 3; i++) { + for (let j = 0; j < bigArray[i].length; j++) { + let product = + bigArray[i][j] * + bigArray[i + 1][j] * + bigArray[i + 2][j] * + bigArray[i + 3][j]; + if (product > maxProduct) { + maxProduct = product; + } + } + } + + return maxProduct; +} + +const result = greatestProduct(matrix); +console.log(result);