diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..a739a2938 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,119 +1,281 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - - +function maxOfTwoNumbers(a, b) { + if (a > b) return a; + else if (a < b) return b; + else return a; +} // 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(words) { + let indexLongest = 0; + let longestWords = []; + if (words.length === 0) { + return null; + } + for (let i = 0; i < words.length; i++) { + if (words[i].length > words[indexLongest].length) { + indexLongest = i; + longestWords = [words[i]]; + } else if (words[i].length === words[indexLongest].length) { + longestWords.push(words[i]); + } + } + return words[indexLongest]; +} // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} - - +function sumNumbers(numbers) { + if (numbers.length === 0) { + return 0; + } + let totalSum = 0; + for (let i = 0; i < numbers.length; i++) { + totalSum = totalSum + numbers[i]; + } + return totalSum; +} // Iteration #3.1 Bonus: -function sum() {} - - +function sum(numbers) { + if (numbers.length === 0) { + return 0; + } + let totalSum = 0; + for (let i = 0; i < numbers.length; i++) { + if (typeof numbers[i] === "number") { + totalSum = totalSum + numbers[i]; + } else if (typeof numbers[i] === "string") { + totalSum = totalSum + numbers[i].length; + } else if (typeof numbers[i] === "boolean") { + if (numbers[i] == true) { + totalSum = totalSum + 1; + } + } else { + throw new Error(`Unsupported data`); + } + } + return totalSum; +} // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} - +function averageNumbers(numbersAvg) { + if (numbersAvg.length === 0) { + return null; + } + let totalSum = 0; + for (let i = 0; i < numbersAvg.length; i++) { + totalSum = totalSum + numbersAvg[i]; + } + return totalSum / numbersAvg.length; +} // 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(wordsArr) { + if (wordsArr.length === 0) { + return null; + } + let totalLength = 0; + for (let i = 0; i < wordsArr.length; i++) { + totalLength = totalLength + wordsArr[i].length; + } + return totalLength / wordsArr.length; +} // Bonus - Iteration #4.1 -function avg() {} +const mixedArr = [6, 12, "miami", 1, true, "barca", "200", "lisboa", 8, 10]; +function avg(mixedArr) { + if (!mixedArr.length) { + return null; + } + let totalLength = 0; + for (let i = 0; i < mixedArr.length; i++) { + if (typeof mixedArr[i] === "string") { + totalLength = totalLength + mixedArr[i].length; + } else if (typeof mixedArr[i] === "number") { + totalLength = totalLength + mixedArr[i]; + } else if (typeof mixedArr[i] === "boolean") { + if (mixedArr[i] === true) { + totalLength = totalLength + 1; + } + } + } + return totalLength / mixedArr.length; +} // 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(wordsUnique) { + let wordsUniqueNoDupl = []; + if (!wordsUnique.length) { + return null; + } + for (let i = 0; i < wordsUnique.length; i++) { + if (wordsUniqueNoDupl.indexOf(wordsUnique[i]) === -1) { + wordsUniqueNoDupl.push(wordsUnique[i]); + } + } + return wordsUniqueNoDupl; + console.log(wordsUniqueNoDupl); + //console.log(wordsUnique.indexOf("crab")); +} // 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(wordlist, wordToSearch) { + if(!wordlist.length){return null;} + let exists = false; + for (let i=0; i