From 20b718c43a3a4f1f3d86a8285b61d11c839a1100 Mon Sep 17 00:00:00 2001 From: Aminmoh9 Date: Thu, 10 Jul 2025 08:45:35 +0200 Subject: [PATCH 1/2] Solved lab. --- src/functions-and-arrays.js | 142 ++++++++++++++++++++++++++++++++---- 1 file changed, 129 insertions(+), 13 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..951ca8087 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,41 +1,127 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(x,y) { + if (x > y) { + return x; + } else { + return y; + } +} +console.log(maxOfTwoNumbers(5,10)); // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(arr) { + if (arr.length === 0) return null; + let longest = ""; + + for (let word of arr) { + if (word.length > longest.length) + longest = word; + +} return longest; + +} +console.log(findLongestWord(words)); // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(arr) { + if(arr.length === 0) return 0; + let sum = 0; + for (let num of arr ){ + sum += num; + } + return sum; + +} +console.log(sumNumbers(numbers)); // Iteration #3.1 Bonus: -function sum() {} - +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; + +function sum(arr) { + if (arr.length === 0) return 0; + + let total = 0; + + for (let item of arr) { + if (typeof item === 'number'){ + total += item; + } else if (typeof item === 'string'){ + total += item.length; + } else if (typeof item === 'boolean'){ + total += item ? 1:0; + } else { + throw new Error ("Unsupported data type in array") + } + } + return total; +} +console.log(sum([])); +console.log(sum([3,5,7])); +console.log(sum(['hello', 'hi'])) +console.log(sum(['hello' , 5, true])); +console.log(sum([true, false, true])); +console.log(sum(mixedArr)); // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(arr) { + if (arr.length === 0) return null; + + let sum = 0; + for (let num of arr) { + sum += num; + } + return sum /arr.length; +} +console.log(averageNumbers(numbersAvg)); // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(arr) { + if (arr.length === 0) return null; + + let totalLength = 0; + for (let word of arr){ + totalLength += word.length; + } + return totalLength /arr.length +} // Bonus - Iteration #4.1 -function avg() {} +function avg(arr) { + if (arr.length === 0) return null; + + let total = 0; + + for (let item of arr) { + if (typeof item === 'number'){ + total += item; + } else if (typeof item === 'string'){ + total += item.length; + } else if (typeof item === 'boolean'){ + total += item ? 1:0; + } else { + throw new Error ("Unsupported data type in array") + } + } + return total / arr.length; +} +console.log(avg(mixedArr)); // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,16 +138,33 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(arr) { + if (arr.length === 0) return null; + const uniqueArr = []; + for (let item of arr){ + if (!uniqueArr.includes(item)){ + uniqueArr.push(item); + } + } return uniqueArr; +} -// Iteration #6: Find elements -const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; +console.log(uniquifyArray(wordsUnique)); -function doesWordExist() {} +// Iteration #6: Find elements +const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience','truth']; + +function doesWordExist(arr,word) { + if (arr.length === 0) return null; + + return arr.includes(word); +} +console.log(doesWordExist(wordsFind, "subset")); +console.log(doesWordExist(wordsFind, "hello")); +console.log(doesWordExist(wordsFind, "truth")); // Iteration #7: Count repetition const wordsCount = [ @@ -78,8 +181,21 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(arr, word) { + if (arr.length === 0) return 0; + + let count = 0; + + for (let item of arr) { + if (item === word){ + count ++; + } + } return count; +} +console.log(howManyTimes(wordsCount, 'matter')); +console.log(howManyTimes(wordsCount, 'machine')); +console.log(howManyTimes(wordsCount, 'book')); // Iteration #8: Bonus From 817891775aa551da8c88d42e157273d187d04585 Mon Sep 17 00:00:00 2001 From: Aminmoh9 Date: Thu, 10 Jul 2025 09:56:34 +0200 Subject: [PATCH 2/2] Minor adjustment --- src/functions-and-arrays.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 951ca8087..38f67ac1c 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -101,6 +101,7 @@ function averageWordLength(arr) { } return totalLength /arr.length } +console.log(averageWordLength(wordsArr)); // Bonus - Iteration #4.1 function avg(arr) {