diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..6a9c73a5d 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,119 +1,289 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - - +function maxOfTwoNumbers(a, b) { + if (a > b) { + return a; + } + return b; +} // 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(newArr) { + let longestWord; + let longest=0; + if(newArr.length===0){ + return null + } + for(let i=0; ilongest){ + longest=newArr[i].length + longestWord=newArr[i]; + } + } +return longestWord +} +findLongestWord(words); // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} - - +function sumNumbers(numbers) { + let sum = 0; + numbers.forEach((e) => { + sum += e; + }); + return sum; +} +sumNumbers(numbers); // Iteration #3.1 Bonus: -function sum() {} - +const mixedArr = [6, 12, "miami", 1, true, "barca", "200", "lisboa", 8, 10]; +function sum(array) { + let sumTheNumbers = 0; + for (let i = 0; i < array.length; i++) { + switch (typeof array[i]) { + case "string": + sumTheNumbers += array[i].length; + break; + case true: + sumTheNumbers += 1; + break; + case false: + sumTheNumbers += 0; + break; + case "object" || "array": + throw new Error("the type no"); + default: + sumTheNumbers += array[i]; + } + } + return sumTheNumbers; +} +console.log(sum(mixedArr)); // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} - +function averageNumbers(avarageArray) { + let result; + if (avarageArray.length === 0) { + return null; + } + return sumNumbers(avarageArray) / avarageArray.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(wordsArr1) { + let sum = 0; + let result = 0; + + if (wordsArr1.length === 0) { + return null; + } + for (let i = 0; i < wordsArr1.length; i++) { + sum += wordsArr1[i].length; + result = sum / wordsArr1.length; + } + + return result; +} + +console.log(averageWordLength(wordsArr)); // Bonus - Iteration #4.1 -function avg() {} +function avg(arr) { + let result=0; + result = arr.length===0 ? null : sum(arr)/arr.length + return result +} // 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(newArr) { + let withoutDuplicateArr=[]; + if(newArr.length===0){ + return null + } + for(let i=0; igreatNumber){ + greatNumber=fourAdj + } + for(let j=0; jgreatNumber){ + greatNumber=fourAdj; + } + + } + } + return greatNumber +} +console.log(greatestProduct(matrix)) // The following is required to make unit tests work. /* Environment setup. Do not modify the below code. */ -if (typeof module !== 'undefined') { +if (typeof module !== "undefined") { module.exports = { maxOfTwoNumbers, findLongestWord, @@ -125,6 +295,6 @@ if (typeof module !== 'undefined') { uniquifyArray, doesWordExist, howManyTimes, - greatestProduct + greatestProduct, }; }