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..78707d501 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -1,6 +1,17 @@ // Find the maximum +function maxOfTwoNumbers(num1, num2){ + if(num1 > num2){ + console.log(num1 + " is greater"); + }else{ + console.log(num2 + " is greater"); + } +} + +maxOfTwoNumbers(245, 124134134) + // Finding Longest Word +let longestWord = ''; var words = [ 'mystery', 'brother', @@ -11,14 +22,45 @@ var words = [ 'crackpot' ]; +function findTheLongestWord(words){ + + for(let i = 0; i < words.length; i++ ){ + if(words[i].length > longestWord.length) + longestWord = words[i]; + } + console.log("The longest word is " + longestWord); +} + +findTheLongestWord(arrayOfWords); + // Calculating a Sum var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; +let counter = 0; + +function sumArray(nums){ + for(let i = 0; i < nums.length; i++) + counter += nums[i]; + console.log(counter); +} + +sumArray(numbers); // Calculate the Average var numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; +let avgCounter = 0; + +function averageNumbers(nums){ + for(let i = 0; i < nums.length; i++){ + avgCounter += nums[i]; + } + avgCounter /= nums.length; + console.log(avgCounter); +} + +averageNumbers(arrayOfNumbers); // Array of Strings var wordsArr = [ 'seat', @@ -33,8 +75,22 @@ var wordsArr = [ 'palace' ]; +let avgWordCounter = 0; + +function averageWordLength(words){ + for(let i = 0; i < words.length; i++){ + avgWordCounter += words[i].length; + + } + avgWordCounter /= words.length; + console.log(avgWordCounter); +} + + +averageWordLength(lengthWords); + // Unique Arrays -var wordsUnique = [ +let uniqueWords = [ 'crab', 'poison', 'contagious', @@ -48,6 +104,21 @@ var wordsUnique = [ 'bring' ]; +let emptyArray = []; + +function uniquifyArray(words){ + for(i = 0; i < words.length; i++){ + if(emptyArray.indexOf(uniqueWords[i])=== -1){ + emptyArray.push(uniqueWords[i]); + } + + +} +console.log(emptyArray); +} + +uniquifyArray(uniqueWords); + // Finding Elements var wordsFind = [ 'machine', @@ -60,6 +131,22 @@ var wordsFind = [ 'disobedience' ]; +function doesWordExist(array,word){ + var find=false; + if (array.length < 1){ + find = false; + return find; + } + else { + array.forEach(function(a){ + if (a === word){ + find = true; + return find; + } + }) + } + return find; + } // Counting Repetion var wordsCount = [ 'machine', @@ -74,6 +161,22 @@ var wordsCount = [ 'disobedience', 'matter' ]; + +function howManyTimes(array,word){ + var counter = 0; + if (array.length < 1){ + var find = false; + return find; + } + else { + array.forEach(function(a){ + if (a === word){ + counter +=1 + } + }) + } + return counter; + } // 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 () {