diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..bb5957193 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,119 +1,299 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(number1, number2) { + if (number1 > number2) { + return number1; + } else { + return number2; + } +} +// Iteration #2: Find longest word +const words = [ + "mystery", + "brother", + "aviator", + "crocodile", + "pearl", + "orchard", + "crackpot", +]; +function findLongestWord(array) { + let LongestWord = ""; -// Iteration #2: Find longest word -const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; + if (array.length === 0) { + return null; + } -function findLongestWord() {} + for (let i = 0; i < array.length; i++) { + const word = array[i]; + if (word.length > LongestWord.length) { + LongestWord = word; + } + } + return LongestWord; +} // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} - +function sumNumbers(arrayNum) { + let sum = 0; + for (let i = 0; i < arrayNum.length; i++) { + sum += arrayNum[i]; + } + return sum; +} // Iteration #3.1 Bonus: -function sum() {} - - +const mixedArr = [6, 12, "miami", 1, true, "barca", "200", "lisboa", 8, 10]; + +function sum(arr) { + let result = 0; + + if (arr.length === 0) { + return 0; + } + + for (let i = 0; i < arr.length; i++) { + const element = arr[i]; + + if (typeof element === "number") { + result += element; + } else if (typeof element === "string") { + result += element.length; + } else if (typeof element === "boolean") { + result += element ? 1 : 0; + } else {throw new Error("tipo de caracter no valido"); + } + + + } + + return result; +} // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(numArr) { +if (numArr.length === 0) +return null; + + return sumNumbers (numArr) / numArr.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(arrWords) { + if (arrWords.length === 0) { + return null; +} + return sum (arrWords) / arrWords.length; ; + +} // Bonus - Iteration #4.1 -function avg() {} +function avg(arr) { + if (arr.length === 0) { + return null; + } + + return sum (arr) / arr.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(arrayMix) { + if (arrayMix.length === 0) { + return null; + } + let newArray = []; -// Iteration #6: Find elements -const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; + for (let i = 0; i < arrayMix.length; i++) { + let wordsUnique = arrayMix[i]; + if (newArray.indexOf(wordsUnique) === -1) { + newArray.push(wordsUnique); + } + } -function doesWordExist() {} + return (newArray); + +} +// Iteration #6: Find elements +const wordsFind = [ + "machine", + "subset", + "trouble", + "starting", + "matter", + "eating", + "truth", + "disobedience", +]; +function doesWordExist(letterArray,searchedWord) { + if (!letterArray.length) return null; + return letterArray.indexOf(searchedWord) !== -1; +} // 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(arra,palabraBuscada) { + let count = 0; + for (let i = 0; i