diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..aa4dc7444 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,38 +1,84 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - +function maxOfTwoNumbers(num1,num2) { + if(num1 > num2){ + return num1 + } else { return num2} +} // Iteration #2: Find longest word -const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; +/*const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(words) { + if (words.length === 0){ + return null + } + let longestWord = words[0]; + + words.forEach(function(word) { + 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]; -function sumNumbers() {} - +function sumNumbers(numbers) { + if(numbers.length === 0){ + return 0 + } + + let numSum = 0 + + numbers.forEach(function(number){ + numSum += number + }) + return numSum +} // Iteration #3.1 Bonus: 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(avgSum){ + if (avgSum.length === 0){ + return null; + } + function sumNumbers(numbersAvg) { + let avgSum = 0; + + numbersAvg.forEach(function (number) { + avgSum += number; + }); + return avgSum; +} + return sumNumbers(numbersAvg) / numbersAvg.length +} // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(arr) { + let lengthSum = 0 + if(arr.length === 0){ + return null; + } + for (let i=0; i