From af1fb9f8c888bbc1d13cf385449b738049961e5e Mon Sep 17 00:00:00 2001 From: Franciely Beckert Date: Mon, 22 Jan 2024 15:59:13 +0100 Subject: [PATCH] Done --- src/functions-and-arrays.js | 180 ++++++++++++++++++++++++++++++++---- 1 file changed, 161 insertions(+), 19 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..ff6206ea5 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,41 +1,140 @@ -// Iteration #1: Find the maximum -function maxOfTwoNumbers() {} + // Iteration #1: Find the maximum + +// Conditional Statement +function maxOfTwoNumbers(num1, num2) { + let maxNum; + if (num1 > num2) { + (maxNum = num1); + } else { + (maxNum = num2); + } + return maxNum; +} +console.log(maxOfTwoNumbers(5, 4)); + +// // ternary operator +function maxOfTwoNumbers(num1, num2) { + return (num1 > num2) ? num1 : num2; +} +console.log(maxOfTwoNumbers(5, 9)); +// The Math.max() Method +function maxOfTwoNumbers(num1, num2) { + let maxNum = Math.max(num1, num2); + return maxNum; +} +console.log(maxOfTwoNumbers(10, 11)); -// Iteration #2: Find longest word +// Iteration #2: Find longest word + const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord() { + let longestWord= ""; + for (let i=0; i longestWord.length){ + longestWord= words[i]; + } + } + return longestWord +} +console.log(findLongestWord()) -// Iteration #3: Calculate the sum -const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +// Iteration #3: Calculate the sum +const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; +function sumNumbers(value) { + let sum = 0; + for (let i = 0; i sum += number) +console.log(sum) + +// reduce +const sumNumbers = numbers.reduce((acc, currValue) => { + return acc + currValue +},0); +console.log(sumNumbers) + +// Iteration #3.2 Bonus: +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; + +function sum(values) { + let totalSum = 0; + for (let i=0; i { average += num / listGiven.length}) + return average +} +console.log(averageNumbers(numbersAvg)); + +// reduce +const arrAvg = arr => arr.reduce((a,b) => a+b,0)/arr.length +console.log(arrAvg(numbersAvg)) // Level 2: Array of strings -const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } -// Bonus - Iteration #4.1 -function avg() {} +const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; + +function averageWordLength(listGiven) { + let averageLength = 0; + listGiven.forEach(word => { averageLength += word.length / listGiven.length + }); + return averageLength +} +console.log(averageWordLength(wordsArr)) + +// Bonus - Iteration #4.3 +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; + +function avg(values) { + let totalAverage= 0; + values.forEach(value => { + if ( typeof value === 'number') { + totalAverage += value; + } else if (typeof value === 'string') { + totalAverage +=value.length; + } else if (typeof value === 'boolean'){ + totalAverage += value ? 1: 0; + } + }); + return totalAverage / values.length; +} +console.log(avg(mixedArr)); // Iteration #5: Unique arrays const wordsUnique = [ @@ -51,15 +150,21 @@ const wordsUnique = [ 'simple', 'bring' ]; +function uniquifyArray(words) { + return words.filter((item, index) => words.indexOf(item) === index); +} -function uniquifyArray() {} +console.log(uniquifyArray(wordsUnique)) // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(arrWords, wordToSearch) { + return arrWords.includes(wordToSearch) ? true : false; +} +console.log(doesWordExist(words, 'machin')) @@ -78,7 +183,21 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(arr, word) { + let count = 0; + arr.forEach(element => { + if (element === word) { + count++ + } + }); + return count +} + +console.log(howManyTimes(wordsCount, "matter")) + +// function howManyTimes(arr, word) { +// return arr.filter(element => element === word).length; +// } @@ -106,7 +225,30 @@ 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; + + // Check horizontal products + for (let i = 0; i < matrix.length; i++) { + for (let j = 0; j <= matrix[i].length - 4; j++) { + const product = matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3]; + maxProduct = Math.max(maxProduct, product); + } + } + + // Check vertical products + for (let i = 0; i <= matrix.length - 4; i++) { + for (let j = 0; j < matrix[i].length; j++) { + const product = matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j]; + maxProduct = Math.max(maxProduct, product); + } + } + + return maxProduct; +} + +console.log(greatestProduct(matrix));