diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..c89ca5288 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,20 +1,47 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - +function maxOfTwoNumbers(a, b) { + if (a > b) { + return a + } + else { + return b + } +} +console.log(maxOfTwoNumbers); // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} - +function findLongestWord(wordsArray) { + if (!wordsArray.length) { + return null + } + let longestWord = "" + for (let i = 0; i < wordsArray.length; i++) { + if ( wordsArray[i].length > longestWord.length) { + longestWord = wordsArray[i] + } + } +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(arr) { + let sum = 0; + for (let i = 0; i < arr.length; i++) { + if (arr.length > 0) { + sum += arr[i];} + } + return sum; +} + +console.log(sumNumbers([numbers])) // Iteration #3.1 Bonus: @@ -26,13 +53,38 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} + +function averageNumbers(avArr) { + if (!avArr.length) { + return null + } + let sum = 0; + for (let i = 0; i < avArr.length; i++) { + if (avArr.length > 0) { + sum += avArr[i];} + } + + return sum / avArr.length; +} +console.log(averageNumbers([numbersAvg])); // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(wordsChar) { +if (!wordsChar.length) { + return null +} +let stringArr = 0; +for (let i = 0; i < wordsChar.length; i++) { +if (wordsChar.length > 0) { + stringArr += wordsChar[i].length + console.log(stringArr); +}} +return stringArr/wordsChar.length +} + // Bonus - Iteration #4.1 function avg() {} @@ -52,8 +104,11 @@ const wordsUnique = [ 'bring' ]; + + function uniquifyArray() {} + // Iteration #6: Find elements