From 114c6618c27d8479e1b9b4b1f916e2dd90c43628 Mon Sep 17 00:00:00 2001 From: Olaf Date: Sat, 21 Sep 2024 09:05:11 +0200 Subject: [PATCH 1/2] Submission of Lab3 --- src/functions-and-arrays.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..2f11e1e7a 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,5 +1,12 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(number1, number2) { + if(number1 > number2){ + return number1; + } + else { + return number2; + } +} From 8b2cdac161126ea7c0d85daf2211125e3be95e47 Mon Sep 17 00:00:00 2001 From: Olaf Date: Sat, 21 Sep 2024 09:11:55 +0200 Subject: [PATCH 2/2] First commit not successful, trying again. --- src/functions-and-arrays.js | 305 ++++++++++++++++++++++++++++++++++-- 1 file changed, 293 insertions(+), 12 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 2f11e1e7a..bbe4e7dd1 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -12,20 +12,112 @@ function maxOfTwoNumbers(number1, number2) { // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; +const empty = []; + +findLongestWord(empty) + +function findLongestWord(words) +{ + if (words.length === 0) + { + return null; + } + else if (words.length === 1) + { + return words[0]; + } + else { + return words.sort ((a, b) => b.length - a.length)[0]; + } + +} -function findLongestWord() {} // 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) + { + for (let i = 0; i < numbers.length; i++) { + + sum = sum + numbers[i]; + + } + } + else { + + sum = 0; + } + return sum; +} +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; + + +let typeOfArray; // Iteration #3.1 Bonus: -function sum() {} +function sum(chaosarray) +{ + let counter = 0; + let totals=0; + if (chaosarray.length !== 0) + { + + + for (let i = 0; i < chaosarray.length; i++) + { + + typeOfArray = typeof chaosarray[i]; + + switch (typeOfArray) + { + case "string": + { + counter = chaosarray[i].length; + totals += counter; + + } + + break; + + case "number": + totals = totals + (chaosarray[i]); + + break; + + case "boolean": + let booleanTranslate = 0; + + if (chaosarray[i] === true) + { + totals ++; + } + + + + break; + + default: + throw new Error("The value is not a number, string o boolean"); + // break; + } + + } + } + else { + return 0; + } + return totals; +} + + //console.log("Totals from sum function: " + sum(mixedArr)); @@ -33,16 +125,63 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +let result; +function averageNumbers(avg) +{ + if (avg.length !== 0){ + result = parseInt(sumNumbers(avg)) / avg.length; +} + else { + result = null; + } + return result; +} +//console.log("Result final:" + result); // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; - -function averageWordLength() { } +let totalwordlength; + +function averageWordLength(wordsarray) +{ + totalwordlength = 0; + + if(wordsarray.length !== 0) + { + for(i=0; i