From e3b99b8bd9207043f99c4fa0d5af7e348becc481 Mon Sep 17 00:00:00 2001 From: Williams Avila Date: Sat, 22 Feb 2025 21:49:40 +0100 Subject: [PATCH] Solved lab --- .idea/.gitignore | 8 + .idea/lab-javascript-functions-and-arrays.iml | 9 + .idea/misc.xml | 6 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + src/functions-and-arrays.js | 176 ++++++++++++++++-- 6 files changed, 196 insertions(+), 17 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/lab-javascript-functions-and-arrays.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 000000000..13566b81b --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/lab-javascript-functions-and-arrays.iml b/.idea/lab-javascript-functions-and-arrays.iml new file mode 100644 index 000000000..d6ebd4805 --- /dev/null +++ b/.idea/lab-javascript-functions-and-arrays.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..6f29fee2f --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..1bf13e24c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..35eb1ddfb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..73d6f26a7 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,41 +1,132 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - - +function maxOfTwoNumbers(numberOne, numberTwo) { + if (numberOne > numberTwo) { + return numberOne; + } else { + return numberTwo; + } +} -// Iteration #2: Find longest word +// Iteration #2: Find the longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(Array) { + if (Array.length === 0) { + return null; + } + if (Array.length === 1) { + return Array[0]; + } + + let wordLonger = ""; + for ( let word of Array){ + if (word.length > wordLonger.length ) { + wordLonger = word; + return wordLonger; + } + } +} // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(array) { + if (array.length === 0) { + return 0; + } + let sum = 0; -// Iteration #3.1 Bonus: -function sum() {} + for (let i of array) { + if (typeof i === 'number') { + sum += Number(i); + } else if (typeof i === 'boolean') { + sum += i; + } + if (sum === 0 ) { + return 0; + } + } + return sum; + +} + +// Iteration #3.1 Bonus: +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; +function sum(array) { + let sum = 0; + if (array.length === 0) { + return 0; + } else if (array.length !== 0) { + for (let eachOne of array) { + + if (typeof eachOne === 'number') { + sum += Number(eachOne) + } else if (typeof eachOne === 'string') { + sum += Number(eachOne.length) + } else if (typeof eachOne === 'boolean') { + sum += eachOne; + } else if (typeof eachOne === 'object' || Array.isArray(eachOne)) { + throw new Error("Error message goes here"); + } + } + return 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(array) { + + let sum = 0; + if (array.length === 0) { + return null; + } else if (array.length === 1){ + return array[0]/array.length; + } + + for (let eachOne of array) { + if (eachOne < 0 || eachOne > 0) + sum += eachOne; + + } + return sum/array.length; + +} // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(array) { + let sum = 0; + if ( array.length === 0 ) { + return null; + } else if (array.length === 1){ + return array[0].length/array.length; + } else { + for ( let eachOne of array){ + sum += eachOne.length; + } + return sum/array.length; + } +} // Bonus - Iteration #4.1 -function avg() {} +function avg(arr) { + if (arr.length === 0) { + return null; + } else { + return sum(arr)/arr.length; + } +} // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,16 +143,36 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(array) { + if (array.length === 0) { + return null + } + let newArray = []; + for (let i = 0; i < array.length; i++) { + if (array.includes(array[i])){ + newArray.push(array[i]); + } + } + return newArray; +} -// Iteration #6: Find elements -const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +// Iteration #6: Find elements +const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; + +function doesWordExist(array, word) { + if (array.length === 0) { + return null + } else if (array.includes(word)) { + return true; + } else if (!array.includes(word)) { + return false; + } +} // Iteration #7: Count repetition const wordsCount = [ @@ -78,7 +189,28 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(array, word) { + if (array.length === 0) { + return 0; + } else if (!array.includes(word)=== true) { + return 0; + } else if (array.includes(word)) { + let count = 0; + for(let i = 0; i