From f7b71f687dd11fff5f007f37ebd973eff07adef6 Mon Sep 17 00:00:00 2001 From: Stefano Romanelli Date: Sun, 28 Jan 2024 17:29:15 +0100 Subject: [PATCH 01/13] iteration 1 --- src/functions-and-arrays.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..469419d00 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,6 +1,16 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(numOne, numTwo) { + if (numOne > numTwo) { + return numOne; + } else if (numOne < numTwo) { + return numTwo; + } else { + return numOne || numTwo; + } +} + +// console.log(maxOfTwoNumbers(2, 8)) // Iteration #2: Find longest word From 06799b073869e372103165818bfa7b4b3de3c96f Mon Sep 17 00:00:00 2001 From: Stefano Romanelli Date: Sun, 28 Jan 2024 17:29:49 +0100 Subject: [PATCH 02/13] iteration 2 --- src/functions-and-arrays.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 469419d00..d8d2267a9 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -13,10 +13,35 @@ function maxOfTwoNumbers(numOne, numTwo) { // console.log(maxOfTwoNumbers(2, 8)) + // Iteration #2: Find longest word -const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +const words = [ + "mystery", + "brother", + "aviator", + "crocodile", + "pearl", + "orchard", + "crackpot", +]; + +function findLongestWord(words) { + if (words.length == 0) { + return null; + } + + let longWord = ""; + + for (let word of words) { + if (word.length > longWord.length) { + longWord = word; + } + } + return longWord; +} + +// console.log(findLongestWord(words)) From 901cc31d4730698239662e434c09301f18123de6 Mon Sep 17 00:00:00 2001 From: Stefano Romanelli Date: Sun, 28 Jan 2024 17:30:21 +0100 Subject: [PATCH 03/13] iteration 3 --- src/functions-and-arrays.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index d8d2267a9..1c8e691a7 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -48,8 +48,33 @@ 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) { + if (numbers.length === 0) { + return 0; + } + + let pallino = 0; + + for (let i = 0; i < numbers.length; i++) { + pallino += numbers[i]; + } + + return pallino; +} + +/* + +function sumNumbers(arrNum) { + let sum = 0; + for (num of arrNum) + { + sum = sum + num; + } + return sum; +} +console.log(sumNumbers(numbers)) +*/ // Iteration #3.1 Bonus: From 44c49e461a4f2669b394f54744c8415dd7105235 Mon Sep 17 00:00:00 2001 From: Stefano Romanelli Date: Sun, 28 Jan 2024 17:30:54 +0100 Subject: [PATCH 04/13] iteration 3.1 --- src/functions-and-arrays.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 1c8e691a7..c85fa6989 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -78,7 +78,35 @@ console.log(sumNumbers(numbers)) // Iteration #3.1 Bonus: -function sum() {} +const mixedArr = [6, 12, "miami", 1, true, "barca", "200", "lisboa", 8, 10]; + +function sum(mixedArr) { + if (mixedArr.length === 0) { + return 0; + } + + let numberSum = 0; + let stringSum = 0; + let booleanSum = 0; + + for (let i = 0; i < mixedArr.length; i++) { + if (typeof mixedArr[i] === "number") { + numberSum = numberSum += mixedArr[i]; + } else if (typeof mixedArr[i] === "string") { + stringSum = stringSum + mixedArr[i].length; + } else if (typeof mixedArr[i] === "boolean") { + booleanSum = booleanSum += mixedArr[i]; + } else throw new Error("Error message goes here"); + } + + let totalSum = numberSum + stringSum + booleanSum; + + console.log (totalSum) + + return totalSum +} + +// console.log(sum(mixedArr)) From 32de0c05050dd790371b327dc6fa0147a72d7332 Mon Sep 17 00:00:00 2001 From: Stefano Romanelli Date: Sun, 28 Jan 2024 17:31:13 +0100 Subject: [PATCH 05/13] iteration 4 --- src/functions-and-arrays.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index c85fa6989..1093445f7 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -114,7 +114,22 @@ function sum(mixedArr) { // 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 count = 0 + let sum = 0 + + count = count + numbersAvg.length + // should we use count = numbersAvg.length or count = count + numbersAvg.length? + sum = sumNumbers(numbersAvg) + return sum / count; +} + +// console.log(averageNumbers(numbersAvg)) // Level 2: Array of strings From a2093eebea93c46d0e05f5e5352ef0759eb314bb Mon Sep 17 00:00:00 2001 From: Stefano Romanelli Date: Sun, 28 Jan 2024 17:31:49 +0100 Subject: [PATCH 06/13] iteration 4 level 2 --- src/functions-and-arrays.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 1093445f7..92391f557 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -133,9 +133,32 @@ function averageNumbers(numbersAvg) { // Level 2: Array of strings -const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; +const wordsArr = [ + "seat", + "correspond", + "linen", + "motif", + "hole", + "smell", + "smart", + "chaos", + "fuel", + "palace", +]; + +function averageWordLength(wordsArr) { + if (wordsArr.length === 0) { + return null; + } -function averageWordLength() { } + let length = 0 + let count = wordsArr.length + + for (let i = 0; i < wordsArr.length; i++) { + length += wordsArr[i].length; +} + return length / count +} // Bonus - Iteration #4.1 function avg() {} From ca83f26881029436e10155d1540fe63db8c1209d Mon Sep 17 00:00:00 2001 From: Stefano Romanelli Date: Sun, 28 Jan 2024 17:32:32 +0100 Subject: [PATCH 07/13] missing console log commented iteration 4 level 2 --- src/functions-and-arrays.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 92391f557..0991c5bf3 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -160,6 +160,8 @@ function averageWordLength(wordsArr) { return length / count } +// console.log(averageWordLength(wordsArr)) + // Bonus - Iteration #4.1 function avg() {} From e53af04a2f306d9d887a50aa09579956b097e04d Mon Sep 17 00:00:00 2001 From: Stefano Romanelli Date: Sun, 28 Jan 2024 17:32:59 +0100 Subject: [PATCH 08/13] iteration 5 --- src/functions-and-arrays.js | 39 +++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 0991c5bf3..5764bb555 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -167,20 +167,35 @@ function avg() {} // Iteration #5: Unique arrays const wordsUnique = [ - 'crab', - 'poison', - 'contagious', - 'simple', - 'bring', - 'sharp', - 'playground', - 'poison', - 'communion', - 'simple', - 'bring' + "crab", + "poison", + "contagious", + "simple", + "bring", + "sharp", + "playground", + "poison", + "communion", + "simple", + "bring", ]; -function uniquifyArray() {} +function uniquifyArray(wordsUnique) { + if (wordsUnique.length === 0) { + return null; + } + + let uniqueWords = [] + + for (let word of wordsUnique) { + if (!uniqueWords.includes(word)) { + uniqueWords.push(word) + } + } + return uniqueWords +} + +// console.log(uniquifyArray(wordsUnique)) From a764402c7732a2a6c19da49621c8d713016948cd Mon Sep 17 00:00:00 2001 From: Stefano Romanelli Date: Sun, 28 Jan 2024 17:33:23 +0100 Subject: [PATCH 09/13] iteration 6 --- src/functions-and-arrays.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 5764bb555..99974a426 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -200,9 +200,30 @@ function uniquifyArray(wordsUnique) { // Iteration #6: Find elements -const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; +const wordsFind = [ + "machine", + "subset", + "trouble", + "starting", + "matter", + "eating", + "truth", + "disobedience", +]; + +function doesWordExist(wordsFind, wordToSearch) { + if (wordsFind.length === 0) { + return null; + } + + for (let word of wordsFind) { + if (word === wordToSearch) { + return true; + } +} return false; +} -function doesWordExist() {} +// console.log(doesWordExist(wordsFind, "matter")) From 040db955b432ac10e8f28f43a09c637a8c3d46fa Mon Sep 17 00:00:00 2001 From: Stefano Romanelli Date: Sun, 28 Jan 2024 17:33:49 +0100 Subject: [PATCH 10/13] iteration 7 --- src/functions-and-arrays.js | 38 +++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 99974a426..669e192d4 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -229,21 +229,35 @@ function doesWordExist(wordsFind, wordToSearch) { // Iteration #7: Count repetition const wordsCount = [ - 'machine', - 'matter', - 'subset', - 'trouble', - 'starting', - 'matter', - 'eating', - 'matter', - 'truth', - 'disobedience', - 'matter' + "machine", + "matter", + "subset", + "trouble", + "starting", + "matter", + "eating", + "matter", + "truth", + "disobedience", + "matter", ]; -function howManyTimes() {} +function howManyTimes(wordsCount, wordToSearch) { + + if (wordsCount.length === 0) { + return 0; + } + + let count = 0 + + for (let word of wordsCount) { + if (word === wordToSearch) { + count++ + } + } return count +} +// console.log(howManyTimes(wordsCount, "disobedience")) // Iteration #8: Bonus From c3929621ca68f10b9d4de238d1e654a341b2b692 Mon Sep 17 00:00:00 2001 From: Stefano Romanelli Date: Sun, 28 Jan 2024 17:34:26 +0100 Subject: [PATCH 11/13] iteration 8 --- src/functions-and-arrays.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 669e192d4..2047f4337 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -284,7 +284,27 @@ 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 greatestProduct = 0; + + for (let i = 0; i < matrix.length; i++) { + for (let j = 0; j < matrix[i].length; j++) { + if (i <= matrix.length - 4) { + // Calculate product horizontally + let product = matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j]; + if (product > greatestProduct) greatestProduct = product; + } + if (j <= matrix[i].length - 4) { + // Calculate product vertically + let product = matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3]; + if (product > greatestProduct) greatestProduct = product; + } + } + } +return greatestProduct +} + +// console.log(greatestProduct(matrix)) From f6a4be2011ed685e75a49b1c62bd09a158ebbdbe Mon Sep 17 00:00:00 2001 From: Stefano Romanelli Date: Sun, 28 Jan 2024 17:52:20 +0100 Subject: [PATCH 12/13] empty function iteration 4.2 bonus --- src/functions-and-arrays.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 2047f4337..e8ee006ee 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -163,7 +163,16 @@ function averageWordLength(wordsArr) { // console.log(averageWordLength(wordsArr)) // Bonus - Iteration #4.1 -function avg() {} +const numberList = [2, 6, 9, 10, 7, 4, 1, 9]; + + +function avg(numberList) { + if (numberList.length === 0) { + return null; +} return sum / count +} + +console.log(avg(numberList)) // Iteration #5: Unique arrays const wordsUnique = [ From 0da084d1fe4f70cae7837492c230aa7401a365cf Mon Sep 17 00:00:00 2001 From: Stefano Romanelli Date: Sun, 28 Jan 2024 18:07:23 +0100 Subject: [PATCH 13/13] iteration 4.2 bonus --- src/functions-and-arrays.js | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index e8ee006ee..4491e8da1 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -101,8 +101,6 @@ function sum(mixedArr) { let totalSum = numberSum + stringSum + booleanSum; - console.log (totalSum) - return totalSum } @@ -163,16 +161,41 @@ function averageWordLength(wordsArr) { // console.log(averageWordLength(wordsArr)) // Bonus - Iteration #4.1 -const numberList = [2, 6, 9, 10, 7, 4, 1, 9]; +/* +Create function avg(arr) that receives any mixed array and calculates the average. For example, consider an array filled with numbers and/or strings and/or booleans as a mixed array. + +The non-numerical values should be counted as follows: + +Booleans: true counts as 1 and false counts as 0. +Strings: use the string length as the numeric value. +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; + +// should return: 5.7 +*/ + +/* const mixedArr = [6, 12, "miami", 1, true, "barca", "200", "lisboa", 8, 10]; */ -function avg(numberList) { - if (numberList.length === 0) { +function avg(mixedArr) { + if (mixedArr.length === 0) { return null; -} return sum / count } -console.log(avg(numberList)) +let somma = 0 +let count = mixedArr.length + +somma = somma + sum(mixedArr) +/*let sum = 0 +let count = mixed.length + +for (let i = 0; i < numberList.length; i++) { + sum += numberList[i]; +} */ + +return somma / count +} + +// console.log(avg(mixedArr)) // Iteration #5: Unique arrays const wordsUnique = [