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..4d2748f34 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -1,4 +1,14 @@ // Find the maximum +function maxOfTwoNumbers (a, b) { + if (a > b) { + return a; + } else if (b > a) { + return b; + } else{ + // return "They are equal"; + return b + } +} // Finding Longest Word var words = [ @@ -11,14 +21,43 @@ var words = [ 'crackpot' ]; + +function findLongestWord (words) { + var maxLengthWord = words[0].length; + for (var i = 0 ; i < words.length ; i++) { + if (words[i].length > maxLengthWord) { + maxLengthWord = words[i]; + } + } return maxLengthWord ; + } + + console.log(findLongestWord (words)) + // Calculating a Sum var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; +function sumArray(tab) { + var sum = 0; + for (var i = 0; i < tab.length; i++) { + sum += tab[i]; + } + return sum; +} + +console.log(sumArray(numbers)); + // Calculate the Average var numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; +function average (tab) { + var aver = 0; + aver = sumArray(tab) / tab.length ; + return aver ; +} +console.log(average(numbersAvg)); + // Array of Strings var wordsArr = [ 'seat', @@ -33,6 +72,18 @@ var wordsArr = [ 'palace' ]; +function averageWordLength(array) { + var averageWord = 0; + var sum = 0; + for (var i = 0; i < array.length ; i++) { + sum += array[i].length; + } + averageWord = sum/array.length; + return averageWord ; + } + + console.log(averageWordLength(wordsArr)); + // Unique Arrays var wordsUnique = [ 'crab', @@ -48,6 +99,19 @@ var wordsUnique = [ 'bring' ]; +function uniquifyArray(array) { + var array1 = []; + var j = 0; + for (var i = 0; i < array.length; i++) { + if (array1.indexOf(array[i]) <= -1){ + array1[j] = array[i]; + j += 1; + } + } + return array1; +} +console.log(uniquifyArray(wordsUnique)); + // Finding Elements var wordsFind = [ 'machine', @@ -60,6 +124,20 @@ var wordsFind = [ 'disobedience' ]; +function doesWordExist(array, word) { + for (var i = 0; i < array.length; i++){ + if (word == array[i]) { + return true; + } else { + continue; + } + } +return false; +} + +console.log(doesWordExist(wordsFind, word)); + + // Counting Repetion var wordsCount = [ 'machine', @@ -74,6 +152,19 @@ var wordsCount = [ 'disobedience', 'matter' ]; + +function howManyTimes(array, word) { + var occ = 0; + for (var i = 0; i < array.length; i++){ + if (word == array[i]) { + occ += 1; + } + } + return occ ; +} + +console.log(howManyTimes(wordsCount, word)); + // Bonus Quest var matrix = [ diff --git a/starter-code/tests/FunctionsAndArraysSpec.js b/starter-code/tests/FunctionsAndArraysSpec.js index 8fb25ff65..67a3b8bad 100644 --- a/starter-code/tests/FunctionsAndArraysSpec.js +++ b/starter-code/tests/FunctionsAndArraysSpec.js @@ -170,7 +170,7 @@ describe('Counting Repetion - howManyTimes', function () { }); it('returns false with an empty array', function () { - expect(howManyTimes([])).toBe(undefined); + expect(howManyTimes([])).toBe(false); }); it('returns one when the word appears only one time on the array', function () {