From 007b812e89709fdd3454151d80964f45dc818f76 Mon Sep 17 00:00:00 2001 From: Elena Piaggio Date: Sat, 15 Feb 2025 15:27:41 +0100 Subject: [PATCH] ADD solved lab --- src/functions-and-arrays.js | 109 ++++++++++++++++++++++++++++++++---- 1 file changed, 98 insertions(+), 11 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..9922bc625 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,24 +1,62 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(num1, num2) { + if (num1 > num2) { + return num1; + } + return num2; +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(words) { + if (words.length < 1) { + return null; + } else { + let longestWord = ""; + + for (const 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(numbers) { + let total = 0; + for (const number of numbers) { + total += number; + } + return total; +} // Iteration #3.1 Bonus: -function sum() {} +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; + +function sum(mixedArr) { + let sum = 0; + for (const element of mixedArr) { + if (typeof element === "string") { + sum += element.length; + } else if (typeof element === 'number' || typeof element === 'boolean') { + sum += element; + } else { + throw new Error("Unsupported data type"); + } + } + return sum; +} @@ -26,16 +64,37 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(numbersAvg) { + if (numbersAvg.length > 0) { + let total = sumNumbers(numbersAvg); + return total / numbersAvg.length; + } + return null; +} // 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) { + let total = 0; + for (const word of wordsArr) { + total += word.length; + } + return total / wordsArr.length; + } + return null; + +} // Bonus - Iteration #4.1 -function avg() {} +function avg(mixedArr) { + if (mixedArr.length > 0) { + return sum(mixedArr) / mixedArr.length; + } + return null; +} // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,14 +111,30 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(wordsUnique) { + if (wordsUnique.length > 0) { + let newArray = []; + for (const word of wordsUnique) { + if (!newArray.includes(word)) { + newArray.push(word) + } + } + return newArray; + } + return null; +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(wordsFind, wordToSearch) { + if (wordsFind.length > 0) { + return wordsFind.includes(wordToSearch); + } + return null; +} @@ -78,7 +153,19 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(wordsCount, wordToSearch) { + if (wordsCount.length == 0) { + return 0; + } + + let total = 0; + for (const word of wordsCount) { + if (word === wordToSearch) { + total++; + } + } + return total; +} @@ -106,7 +193,7 @@ 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() { }