From 2d4cff603c0c3a9a328279e82203df6a2bafe175 Mon Sep 17 00:00:00 2001 From: Laura Musso Date: Sat, 27 Jan 2024 22:31:02 +0100 Subject: [PATCH 1/2] sat2231 laura --- src/functions-and-arrays.js | 144 ++++++++++++++++++++++++++++++++---- 1 file changed, 130 insertions(+), 14 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..18e80e0db 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,41 +1,112 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - +function maxOfTwoNumbers(firstNum, secondNum) { + + if (firstNum > secondNum) { + return firstNum; + } + else if (firstNum < secondNum) { + return secondNum; + } + else { + return firstNum || secondNum; + } +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(arrayWords) { + + let longestWord = ""; + + if (arrayWords.length == 0) + return null; + for (let word of arrayWords) { + if (word.length > longestWord.length) { + longestWord = word; + } + } + return longestWord; +} + +console.log(findLongestWord(words)) + // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; +const numbersVuota = [""]; + +function sumNumbers(arrNum) { + let sum = 0; + for (num of arrNum) { + sum = sum + num; + } + return sum; +} -function sumNumbers() {} - +//console.log(sumNumbers(numbersVuota)) // Iteration #3.1 Bonus: -function sum() {} - +function sum() { } // Iteration #4: Calculate the average // 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 somma = 0; + let count = 0; + + count = count + numbersAvg.length; + somma = sumNumbers(numbersAvg); + + return somma / count; +} +//console.log(averageNumbers(numbersAvg)); // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; +//const wordsArr = ['sam', 'laura']; + + +function averageWordLength(wordsArr) { + + let totalLength = 0; + let wordNum = 0; + + if (wordsArr.length === 0) { + return null; + } + for (let i = 0; i < wordsArr.length; i++) { + totalLength += wordsArr[i].length; + } + + wordNum = wordsArr.length; + return totalLength / wordNum; +} + +console.log(averageWordLength(wordsArr)); + + + + -function averageWordLength() { } // Bonus - Iteration #4.1 -function avg() {} +function avg() { } + + + // Iteration #5: Unique arrays const wordsUnique = [ @@ -51,15 +122,46 @@ const wordsUnique = [ 'simple', 'bring' ]; +function uniquifyArray(wordsUnique) { + + let uniqueArray = []; -function uniquifyArray() {} + if (wordsUnique.length === 0) { + return null; + } + for (let i = 0; i < wordsUnique.length; i++) { + if (uniqueArray.indexOf(wordsUnique[i]) == -1) { + uniqueArray.push(wordsUnique[i]) + } + } + return (uniqueArray); +} +console.log(uniquifyArray(wordsUnique)); // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(wordsFind, wordToFind) { + + if (wordsFind.length === 0) { + return null; + } + /*for (let i = 0; i < wordsFind.length; i++) { + if (wordsFind[i] === wordToFind) + return true; + } + return false;*/ + + if (wordsFind.indexOf(wordToFind) === -1) { + return false + } else { + return true + } +} + +console.log(doesWordExist(wordsFind, 'machine')) @@ -78,7 +180,21 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(wordsCount, wordToSearch) { + + let count = 0; + + if(wordsCount.length === 0) { + return 0; + } + + for(let i = 0; i < wordsCount.length; i++) + { + if (wordsCount[i] === wordToSearch) + count++; + } + return(count); + } @@ -106,7 +222,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() { } From 042ae75f074a677f78c4e28796028f1dd41d636a Mon Sep 17 00:00:00 2001 From: Laura Musso Date: Tue, 30 Jan 2024 17:35:40 +0100 Subject: [PATCH 2/2] mar1732 laura --- src/functions-and-arrays.js | 59 +++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 18e80e0db..376a1ce93 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -30,7 +30,7 @@ function findLongestWord(arrayWords) { return longestWord; } -console.log(findLongestWord(words)) +//console.log(findLongestWord(words)) @@ -50,8 +50,15 @@ function sumNumbers(arrNum) { //console.log(sumNumbers(numbersVuota)) -// Iteration #3.1 Bonus: -function sum() { } +// Iteration #3.2 Bonus: + +const mixArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; + +function sum(numStrArr) { + if (numStrArr.length === 0){ + return null; + } + } // Iteration #4: Calculate the average @@ -72,6 +79,7 @@ function averageNumbers(numbersAvg) { return somma / count; } + //console.log(averageNumbers(numbersAvg)); // Level 2: Array of strings @@ -102,11 +110,37 @@ console.log(averageWordLength(wordsArr)); -// Bonus - Iteration #4.1 -function avg() { } +// Bonus - Iteration #4.3 + +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; + +function avg(arr) { + if (arr.length === 0) { + return null; + } + let contador = 0; + + for (let i = 0; i < arr.length; i++) { + let elementType = typeof arr[i]; + if(elementType === "number"){ + contador = contador + arr[i]; + } + else if (elementType === "string"){ + contador = contador + arr[i].length; + } + else if (elementType === "boolean"){ + if(arr[i] === true){ + contador = contador + 1; + } + } + } + return contador / arr.length; +} +console.log(avg(mixedArr)) +//prendi un array e per ogni elemento verifica il typeOf e poi aggiugi un count // Iteration #5: Unique arrays const wordsUnique = [ @@ -181,20 +215,19 @@ const wordsCount = [ ]; function howManyTimes(wordsCount, wordToSearch) { - + let count = 0; - - if(wordsCount.length === 0) { + + if (wordsCount.length === 0) { return 0; } - for(let i = 0; i < wordsCount.length; i++) - { + for (let i = 0; i < wordsCount.length; i++) { if (wordsCount[i] === wordToSearch) - count++; + count++; } - return(count); - } + return (count); +}