diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..ff7a1070d 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,19 +1,44 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(a,b) { + if (a>b){ return a} + else if (aa.length-b.length)[0] + let longest=0 + for (let i=1; iarray[longest].length){longest=i}} + return array[longest] + } +} // 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} + else if(numbers.length===1){return numbers[0]} + else if(numbers.length>1){ + let a=0 + for (let i=0;i<=numbers.length-1;i++){ + a=a+numbers[i] + } + return a + } + +} @@ -26,13 +51,22 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(numbers) {if (numbers.length===0){return null} +else{return sumNumbers(numbers)/numbers.length}} // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(words) { if(words.length===0){return null} +if (words.length===1){return words[0].length} +else{ + let sumLength=0 + for (let i=0;i<=words.length-1;i++){ + sumLength=sumLength+words[i].length + } + return sumLength/words.length +}} // Bonus - Iteration #4.1 function avg() {} @@ -52,14 +86,30 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(words) { + if (words.length===0){return null} + else{ + for(let i=0;i