From 9c26ff0d49789622b9142031f489544590e2c4fa Mon Sep 17 00:00:00 2001 From: Carol-88 Date: Wed, 18 Jun 2025 13:29:50 +0200 Subject: [PATCH] Solved lab --- src/functions-and-arrays.js | 91 +++++++++++++++++++++++++++++++++---- 1 file changed, 81 insertions(+), 10 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..6e3b3ee7c 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,24 +1,55 @@ // 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, current) => + current.length > longest.length ? current : longest + ); +} // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(numbers) { + if (numbers.length === 0) return 0; + let sum = 0; + for (let i = 0; i < numbers.length; i++) { + sum += numbers[i]; + } + return sum; +} // Iteration #3.1 Bonus: -function sum() {} +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; + +function sum(mixedArr) { + let totalSum = 0; + if (mixedArr.length === 0) return 0; + for (let i = 0; i < mixedArr.length; i++) { + if (typeof mixedArr[i] === 'number') { + totalSum += mixedArr[i]; + } else if (typeof mixedArr[i] === 'string') { + totalSum += mixedArr[i].length; + } else if (typeof mixedArr[i] === 'boolean') { + totalSum += mixedArr[i] ? 1 : 0; + } else { + throw new Error("Error message goes here"); + } + } + return totalSum; +} @@ -26,16 +57,34 @@ 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) return null; + let sum = sumNumbers(numbersAvg); + return sum / numbersAvg.length; +} // 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) return null; + let totalLength = 0; + if (wordsArr.length > 0) { + for (let i = 0; i < wordsArr.length; i++) { + totalLength += wordsArr[i].length; + } + return totalLength / wordsArr.length; + } +}; + // Bonus - Iteration #4.1 -function avg() {} +function avg(mixedArr) { + if (mixedArr.length === 0) return null; + let total = sum(mixedArr); + return total / mixedArr.length; +} // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,14 +101,27 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(wordsUnique) { + if (wordsUnique.length === 0) return null; + let uniqueWords = []; + for (let i = 0; i < wordsUnique.length; i++) { + if (!uniqueWords.includes(wordsUnique[i])) { + uniqueWords.push(wordsUnique[i]); + } + } + return uniqueWords +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(wordsFind, word) { + if (!wordsFind || wordsFind.length === 0) return null; + return wordsFind.includes(word); +} + @@ -78,7 +140,16 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(wordsCount, word) { + if (!wordsCount || wordsCount.length === 0) return 0; + let count = 0; + for (let i = 0; i < wordsCount.length; i++) { + if (wordsCount[i] === word) { + count++; + } + } + return count; +}