diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 467e533bb..ac6080476 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -1,16 +1,130 @@ // Iteration #1: Find the maximum +function maxOfTwoNumbers(min, max) { + + if (min > max) { + + return min; + + } else { + + return max; + + + } +} + +console.log(`Iteration #1: The biggest number is: ${maxOfTwoNumbers(10, 15)}`); + + + + // Iteration #2: Find longest word -const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; +const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot', 'christmas']; + + +function findLongestWord(wordArr) { + + let longestWord; + let longestLength = 0; + + + if (wordArr === undefined || wordArr.length == 0) { + + return null; + + } else if (wordArr.length === 1) { + + return wordArr[0]; + + } else { + + for (let i = 0; i < wordArr.length; i++) { + + if (wordArr[i].length > longestLength) { + + longestLength = wordArr[i].length; + longestWord = wordArr[i]; + + } + } + + } + return longestWord; +} +console.log(`Iteration #2 | The longest word in the array is: ${findLongestWord(words)}`); + + + // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; + +function sumArray(arrWords) { + + let sum = 0; + + if (arrWords === undefined || arrWords.length == 0) { + + return 0; + + } else if (arrWords.length === 1) { + + return arrWords[0]; + + + } else { + + + for (let i = 0; i < arrWords.length; i++) { + sum += arrWords[i]; + } + + } + return sum; +} + +console.log(`Iteration #3: The sum of the numbers in the array is: ${sumArray(numbers)}`); + + + + // Iteration #4: Calculate the average + // Level 1: Array of numbers + const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; +function averageNumbers(arrNumbers) { + + if (arrNumbers === undefined || arrNumbers.length === 0) { + + return null; + + } else if (arrNumbers.length === 1) { + + return arrNumbers[0]; + + } + + + let sumNum = 0; + + arrNumbers.forEach(function (num) { + sumNum += num; + }) + + return sumNum / arrNumbers.length; + + + +} + +console.log(`Iteration #4, level 1 | The average of the numbers in the array is: ${averageNumbers(numbersAvg)}`); + + // Level 2: Array of strings const wordsArr = [ 'seat', @@ -25,6 +139,29 @@ const wordsArr = [ 'palace' ]; + +function averageWordLength(arrWords) { + + if (arrWords === undefined || arrWords.length === 0) { + + return null; + + } + + + let wordLengthSum = 0; + arrWords.forEach(function (value) { + + wordLengthSum += value.length; + + }) + + return wordLengthSum / arrWords.length; + +} + +console.log(`Iteration #4, level 2 | The average word length in the array is: ${averageWordLength(wordsArr)}`); + // Iteration #5: Unique arrays const wordsUnique = [ 'crab', @@ -40,6 +177,26 @@ const wordsUnique = [ 'bring' ]; + +function uniquifyArray(uniqueWordsArr) { + + const uniqueArray = []; + + uniqueWordsArr.forEach(function (value) { + + if (uniqueArray.indexOf(value) === -1) { + uniqueArray.push(value); + + } + + }) + + return uniqueArray; + +} + +console.log(`Iteration #5 | This is an unique array of words: ${uniquifyArray(wordsUnique)}`); + // Iteration #6: Find elements const wordsFind = [ 'machine', @@ -52,6 +209,36 @@ const wordsFind = [ 'disobedience' ]; +function doesWordExist(findWordArr, word) { + +if (findWordArr.length === 0 || findWordArr === undefined) { + +return false; + +} + +else { + + for (let i = 0; i < findWordArr.length; i++) { + + if (findWordArr[i] === word) { + + return true; + + + } + + } + +} + + return false; + +} + +console.log(`Iteration #6 | Does word exist?: ${doesWordExist(wordsFind,'truth')}`); + + // Iteration #7: Count repetition const wordsCount = [ 'machine', @@ -67,6 +254,26 @@ const wordsCount = [ 'matter' ]; + +function howManyTimes(wordCountArr, word) { + + let wordCountArray = []; + + + wordCountArr.forEach(function (value) { + + if (word === value) + + wordCountArray.push(value) + + }) + + return wordCountArray.length; + +} + +console.log(`Iteration #7 | The word is found ${howManyTimes(wordsCount, 'matter')} time(s) in the array`); + // Iteration #8: Bonus const matrix = [ @@ -91,3 +298,10 @@ const matrix = [ [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54], [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] ]; + + +function greatestProduct(){ + + + +} \ No newline at end of file