diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index ce8695c..77fb3d7 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,5 +1,16 @@ // Iteration 1 | Find the Maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(num1,num2) { + if (num1 > num2){ + return num1; + } + else if (num1 < num2){ + return num2; + } + else{ + return num1; + } + +} @@ -7,7 +18,24 @@ function maxOfTwoNumbers() {} // Iteration 2 | Find the Longest Word const words = ["mystery", "brother", "aviator", "crocodile", "pearl", "orchard", "crackpot"]; -function findLongestWord() {} +function findLongestWord(arr) { + let indexOfLongest = -1 ; + let lengthOfLongest = 0 ; + for(i=0 ; i < arr.length ; i++ ){ + if(arr[i].length > lengthOfLongest){ + lengthOfLongest = arr[i].length + indexOfLongest = i + } + } + + if (indexOfLongest === -1){ + return null ; + } + else { + return arr[indexOfLongest]; + } + +} @@ -15,7 +43,13 @@ function findLongestWord() {} // Iteration 3 | Sum Numbers const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(arr) { + let accum = 0 ; + for(i=0 ; i < arr.length ; i++ ){ + accum += arr[i] + } +return accum; +} @@ -23,7 +57,16 @@ function sumNumbers() {} // Iteration 4 | Numbers Average const numbers2 = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(arr) { + if (arr.length !== 0 && Array.isArray(arr) ) { + return sumNumbers(arr) / arr.length; + } + else { + return 0 ; + } + + +} @@ -31,4 +74,23 @@ function averageNumbers() {} // Iteration 5 | Find Elements const words2 = ["machine", "subset", "trouble", "starting", "matter", "eating", "truth", "disobedience"]; -function doesWordExist() {} +function doesWordExist(arr, word) { + let isExists = false ; + if (arr.length !== 0 && Array.isArray(arr) ) { + for(i=0; i < arr.length ; i++) { + if( arr[i] === word ){ + isExists = true ; + break ; + } + } + + } + else { + isExists = null ; + } + return isExists; +} + + + +// arr.forEach((item,word)=>{ if (item === word) { isExists = true; } else { isExists = false; } } )