diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..ff2b0dd41 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,11 +193,24 @@ 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; +} // 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],