diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index ce8695c..d18bdd5 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,34 +1,81 @@ // Iteration 1 | Find the Maximum -function maxOfTwoNumbers() {} - - - +function maxOfTwoNumbers(num1, num2) { + if (num1 > num2) { + return num1; + } else { + return num2; + } +} // Iteration 2 | Find the Longest Word -const words = ["mystery", "brother", "aviator", "crocodile", "pearl", "orchard", "crackpot"]; - -function findLongestWord() {} - - - +const words = [ + "mystery", + "brother", + "aviator", + "crocodile", + "pearl", + "orchard", + "crackpot", +]; + +function findLongestWord(arr) { + if (arr.length === 0) { + return null; + } + let word = ""; + for (let i = 0; i < arr.length; i++) { + if (word.length < arr[i].length) { + word = arr[i]; + } + } + return word; +} // Iteration 3 | Sum Numbers const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} - - - +function sumNumbers(arr) { + let result = 0; + for (let i = 0; i < arr.length; i++) { + result += arr[i]; + } + return result; +} // Iteration 4 | Numbers Average const numbers2 = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} - - - +function averageNumbers(arr) { + if (arr.length === 0) { + return 0; + } + let result = 0; + for (let i = 0; i < arr.length; i++) { + result += arr[i]; + } + return result / arr.length; +} // Iteration 5 | Find Elements -const words2 = ["machine", "subset", "trouble", "starting", "matter", "eating", "truth", "disobedience"]; - -function doesWordExist() {} +const words2 = [ + "machine", + "subset", + "trouble", + "starting", + "matter", + "eating", + "truth", + "disobedience", +]; + +function doesWordExist(wordsToSearch, word) { + if (wordsToSearch.length === 0) { + return null; + } + for (let i = 0; i < wordsToSearch.length; i++) { + if (wordsToSearch[i] === word) { + return true; + } + } + return false; +}