diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..b19f295cf 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,119 +1,310 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - - +function maxOfTwoNumbers(num1, num2) { + if (num1 > num2) { + return num1; + } else { + return num2; + } +} // Iteration #2: Find longest word -const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; - -function findLongestWord() {} - +const words = [ + "mystery", + "brother", + "aviator", + "crocodile", + "pearl", + "orchard", + "crackpot", +]; +function findLongestWord(array) { + let newArray = []; // + if (array.length === 0) { + return null; + } else if (array.length === 1) { + return array[0]; + } else { + for (i = array.length - 1; i >= 0; i--) { + if (newArray.length === 0) { + newArray.push(array[i]); + } else { + if ( + array[i].length > newArray[0].length || + array[i].length === newArray[0].length + ) { + newArray.unshift(array[i]); + } else { + newArray.push(array[i]); + } + } + } + } + return newArray[0]; +} // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} - - - -// Iteration #3.1 Bonus: -function sum() {} - +function sumNumbers(array) { + let sum = 0; + for (i = 0; i < array.length; i++) { + sum += array[i]; + } + return sum; +} +// Iteration #3.2 Bonus: +function sum(array) { + if (array.length === 0) { + return 0 + } + + let finalSum = 0; + for (let i = 0; i < array.length; i++) { + if (typeof array[i] === 'number') { + finalSum += array[i] + } else if (typeof array[i] === 'string') { + finalSum += array[i].length + } else if (typeof array[i] === 'boolean') { + if (array[i] === true) { + finalSum += 1; + } else if (array[i] === false) { + finalSum += 0 + } + } else { + throw new Error("Error message goes here"); + } + } return finalSum +} // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} - +function averageNumbers(array) { + let average = 0 + if (array.length === 0) { + return null + } else if (array.length === 1) { + return array[0] + } else { + average = sumNumbers(array)/array.length + return average + } +} // Level 2: Array of strings -const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; +const wordsArr = [ + "seat", + "correspond", + "linen", + "motif", + "hole", + "smell", + "smart", + "chaos", + "fuel", + "palace", +]; -function averageWordLength() { } +function averageWordLength(array) { + let sum = 0 + if(array.length === 0) { + return null + } -// Bonus - Iteration #4.1 -function avg() {} + for (let i = 0; i < array.length; i++) { + sum += array[i].length + } + + const averageLength = sum / array.length + return averageLength +} + + +// Bonus - Iteration #4.3 +function avg(array) { + if (array.length === 0) { + return null + } + + let finalSum = 0; + let averageSum = 0 + for (let i = 0; i < array.length; i++) { + if (typeof array[i] === 'number') { + finalSum += array[i] + } else if (typeof array[i] === 'string') { + finalSum += array[i].length + } else if (typeof array[i] === 'boolean') { + if (array[i] === true) { + finalSum += 1; + } else if (array[i] === false) { + finalSum += 0 + } + } + } averageSum = finalSum/array.length; + return averageSum +} // Iteration #5: Unique arrays const wordsUnique = [ - 'crab', - 'poison', - 'contagious', - 'simple', - 'bring', - 'sharp', - 'playground', - 'poison', - 'communion', - 'simple', - 'bring' + "crab", + "poison", + "contagious", + "simple", + "bring", + "sharp", + "playground", + "poison", + "communion", + "simple", + "bring", ]; -function uniquifyArray() {} - - +function uniquifyArray(array) { + if (array.length === 0) { + return null + } + + let newArray = []; + array.forEach((element) => { + if (!newArray.includes(element)) { + newArray.push(element) + } + }); return newArray +} // Iteration #6: Find elements -const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; - -function doesWordExist() {} +const wordsFind = [ + "machine", + "subset", + "trouble", + "starting", + "matter", + "eating", + "truth", + "disobedience", +]; +function doesWordExist(array, word) { + if (array.length === 0) { + return null + } else if (array.length === 1 && array.indexOf(word) !== -1) { + return true + } else { + if (array.indexOf(word) !== -1) { + return true + } else { + return false + } + } +} // Iteration #7: Count repetition const wordsCount = [ - 'machine', - 'matter', - 'subset', - 'trouble', - 'starting', - 'matter', - 'eating', - 'matter', - 'truth', - 'disobedience', - 'matter' + "machine", + "matter", + "subset", + "trouble", + "starting", + "matter", + "eating", + "matter", + "truth", + "disobedience", + "matter", ]; -function howManyTimes() {} - - +function howManyTimes(array, word) { + let count = 0; + if (array.length === 0) { + return 0 + } else { + if (array.indexOf(word) !== -1) { + for (let i = 0; i