From f7211e7444eae25a64e7677f135291ce3b059fe3 Mon Sep 17 00:00:00 2001 From: Oswaldo Aviles Date: Mon, 4 Oct 2021 08:45:05 -0500 Subject: [PATCH 1/2] done without bonus & npm revision --- src/functions-and-arrays.js | 151 +++++++++++++++++++++++++++++++++--- 1 file changed, 139 insertions(+), 12 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..52226e74e 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,41 +1,134 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(num1, num2) { + if(num1 > num2){ + return num1; + } else if (num1 < num2){ + return num2; + } else if (num1 === num2){ + return num1; + } +} + // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(arr) { + let longestWord = ""; + if(!arr.length){ + longestWord = null; + } + if(arr.length) { + for(let i = 0; i < arr.length; i++){ + if(arr[i].length > longestWord.length){ + longestWord = arr[i] + } + } + } + return longestWord + } + // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} - +function sumNumbers(arr) { + let totalSum = 0; + for(let i = 0; i < arr.length; i++){ + totalSum += arr[i]; + } + return totalSum; +} // Iteration #3.1 Bonus: -function sum() {} - +function sum(arr) { + let totalSum = 0; + for(let i = 0; i < arr.length; i++){ + if(typeof arr[i] === "number"){ + totalSum += arr[i] + } else if (typeof arr[i] === "string"){ + totalSum += arr[i].length; + } else if (typeof arr[i] === "boolean"){ + if(arr[i] === true){ + totalSum += true; + } else { + totalSum += false; + } + } else if (typeof arr[i] === "object"){ + totalSum = "Unsupported data type sir or ma'am" + } + } + return totalSum; + } // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(arr) { + let totalSum = 0; + let totalElements = arr.length; + if(!arr.length) { + return null; + } + if(arr.length){ + for(let i = 0; i < arr.length; i++){ + totalSum += arr[i]; + } + } + return totalSum / totalElements +} // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(arr) { + let totalSum = 0; + let totalElements = arr.length; + if(!arr.length){ + return null + } + if (arr.length){ + for(let i = 0; i < arr.length; i++){ + totalSum += arr[i].length + } + } + return totalSum / totalElements; +} // Bonus - Iteration #4.1 -function avg() {} +function sum(arr) { + let totalSum = 0; + let totalElements = arr.length; + if(!arr.length){ + return null; + } + if(arr.length){ + for(let i = 0; i < arr.length; i++){ + if(typeof arr[i] === "number"){ + totalSum += arr[i] + } else if (typeof arr[i] === "string"){ + totalSum += arr[i].length; + } else if (typeof arr[i] === "boolean"){ + if(arr[i] === true){ + totalSum += true; + } else { + totalSum += false; + } + } else if (typeof arr[i] === "object"){ + totalSum = "Unsupported data type sir or ma'am" + } + } + } + return totalSum / totalElements; + } // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,14 +145,36 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(arr) { + let newArr = [] + if(!arr.length){ + return null + } + if(arr.length){ + for(let i = 0; i < arr.length; i++){ + if(!newArr.includes(wordsUnique[i])){ + newArr.push(arr[i]); + } + } + } + return newArr; +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(arr, word) { + if(!arr.length){ + return null; + } + if (arr.includes(word)){ + return true; + } else { + return false + } +} @@ -78,7 +193,19 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(arr, word) { + let totalRepetitions = 0; + if(!arr.length || !arr.includes(word)){ + return 0; + } + if (arr.length){} + for(let i = 0; i < arr.length; i++){ + if(arr[i] === word) { + totalRepetitions += 1; + } + } + return totalRepetitions; +} From 37e8a6b075acb9b11b613bf9fb1690458689241c Mon Sep 17 00:00:00 2001 From: Oswaldo Aviles Date: Mon, 4 Oct 2021 09:14:45 -0500 Subject: [PATCH 2/2] Terminado y revisado con NPM test:watch --- src/functions-and-arrays.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 52226e74e..ff2b0dd41 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -210,6 +210,7 @@ function howManyTimes(arr, word) { // Iteration #8: Bonus +// const matrix = [ [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8], [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],