From e7a56f63777559657bc85c4ddb26e7749c1650ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Marc=C3=A9?= Date: Sat, 27 Jan 2024 17:44:17 +0100 Subject: [PATCH 1/5] iteration num.1 --- src/functions-and-arrays.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..592169c6e 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,6 +1,13 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - +function maxOfTwoNumbers(NumA, NumB) { + if (NumA > NumB) { + return NumA; + } else if (NumB > NumA) { + return NumB; + } else { + return NumA; + } +} // Iteration #2: Find longest word From 76399c33901095be26d68b4d53a6fc298b8378bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Marc=C3=A9?= Date: Sat, 27 Jan 2024 18:14:00 +0100 Subject: [PATCH 2/5] iteration 2 --- src/functions-and-arrays.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 592169c6e..888adb9ad 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -9,12 +9,24 @@ function maxOfTwoNumbers(NumA, NumB) { } } - // 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; + } + + let longestWord = ''; + for (let i = 0; i < words.length; i++) { + if (words[i].length > longestWord.length) { + longestWord = words[i]; + } + } + + return longestWord; +} // Iteration #3: Calculate the sum From df99f00c550812cdb5dd4169808cce581a74d713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Marc=C3=A9?= Date: Sat, 27 Jan 2024 19:35:44 +0100 Subject: [PATCH 3/5] iteration 3 finished --- src/functions-and-arrays.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 888adb9ad..de05e79ca 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -30,11 +30,22 @@ function findLongestWord(words) { // Iteration #3: Calculate the sum + const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(numbers) { + let sum = 0; + if (numbers.length === 0) { + return 0; + } + for (let i = 0; i < numbers.length; i += 1) { + sum += numbers[i]; + } + return sum; +} +console.log(sumNumbers(numbers)); // Iteration #3.1 Bonus: function sum() {} From 5e397eaf2b18250ea607a31497523f4df33590d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Marc=C3=A9?= Date: Sun, 28 Jan 2024 12:43:33 +0100 Subject: [PATCH 4/5] iterations 4 and 5 --- src/functions-and-arrays.js | 55 ++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index de05e79ca..0a47fd88b 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -48,7 +48,6 @@ function sumNumbers(numbers) { console.log(sumNumbers(numbers)); // Iteration #3.1 Bonus: -function sum() {} @@ -56,13 +55,46 @@ 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 total = 0; + + for (let i = 0; i < numbersAvg.length; i += 1) { + total += numbersAvg[i]; + } + +const average = total / numbersAvg.length; + +return average; +} + +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(wordsArr) { + if (wordsArr.length === 0) { + return null; + } + + let totalLength = 0; + + for (let i = 0; i < wordsArr.length; i++) { + totalLength += wordsArr[i].length; + } + + const averageLength = totalLength / wordsArr.length; + + return averageLength; +} + +console.log(averageWordLength(wordsArr)); + // Bonus - Iteration #4.1 function avg() {} @@ -82,7 +114,22 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(wordsUnique) { + if (wordsUnique.length === 0) { + return null; + } + + const uniqueArray = []; + + for (let i = 0; i < wordsUnique.length; i++) { + if (!uniqueArray.includes(wordsUnique[i])) { + uniqueArray.push(wordsUnique[i]); + } +} +return uniqueArray +} + +console.log(uniquifyArray(wordsUnique)); From 8e298823c1005974c4593917b94aee56466caf85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Marc=C3=A9?= Date: Tue, 30 Jan 2024 17:37:31 +0100 Subject: [PATCH 5/5] iteration 6 and 7 --- src/functions-and-arrays.js | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 0a47fd88b..f6903809a 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -134,13 +134,43 @@ console.log(uniquifyArray(wordsUnique)); // Iteration #6: Find elements +function doesWordExist(wordsArray, wordToFind) { + if (wordsArray.length === 0) { + return null; + } + + if (wordsArray.includes(wordToFind)) { + return true; + } else { + return false; + } +} + const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; +const wordToFind = 'machine'; + +console.log(doesWordExist(wordsFind, wordToFind)); -function doesWordExist() {} // Iteration #7: Count repetition + +function howManyTimes(wordsCount, searchForWord) { + if (wordsCount.length === 0) { + return 0; + } + + let count = 0; + + for (let i = 0; i < wordsCount.length; i++) { + if (wordsCount[i] === searchForWord) { + count++; + } + } + + return count; +} const wordsCount = [ 'machine', 'matter', @@ -155,7 +185,8 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +const searchForWord = 'matter'; +console.log(howManyTimes(wordsCount, searchForWord));