diff --git a/README.md b/README.md index 5d11b9e6e..81bf62d26 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ Define a function `maxOfTwoNumbers` that takes two numbers as arguments and retu ## Finding Longest Word -Write a function `findLongestWord` that takes an array of words and returns the length of the longest one. If there are 2 with the same length, it should return the first occurrence. +Write a function `findLongestWord` that takes an array of words and returns the longest one. If there are 2 with the same length, it should return the first occurrence. **Starter Code** diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 2fcd81e6e..2a3ba4d11 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -1,5 +1,16 @@ + // Find the maximum +function maxOfTwoNumbers(a,b){ + if (a > b){ + return a; + } + else { + return b; + } + } + + maxOfTwoNumbers(998,89); // Finding Longest Word var words = [ 'mystery', @@ -11,15 +22,64 @@ var words = [ 'crackpot' ]; -// Calculating a Sum +function findLongestWord(words){ + var sortedword = words.sort(function (a,b){ + return b.length - a.length; + }) + return sortedword[0] + } + console.log(findLongestWord(words)) + +// Calculating a Sum +/* var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; +var reducer = (accumulator, currentValue) => accumulator + currentValue; + +console.log(numbers.reduce(reducer)); +*/ + +function sumArray (array){ + curRes=0; + for(var i= 0;i