diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..0bee89e8e 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,24 +1,78 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - - +function maxOfTwoNumbers(number1, number2) { + if (number1 > number2) { + return number1; + } + else if (number1 < number2) { + return number2; + } + else { + return number1; + } +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} - - +function findLongestWord(array) { + let LongestWordIndex = 0; + let LongestWordLength = 0; + if (array.length === 0) { + return null; + } + for(let i = 0; i < array.length; i++) { + if (i === 0) { + LongestWordLength = array[i].length; + } else { + if (array[i].length > LongestWordLength) { + LongestWordIndex = i; + LongestWordLength = array[i].length; + } + } + } + return array[LongestWordIndex]; +} // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} - - +function sumNumbers(array) { + let sum = 0; + for(let i = 0; i < array.length; i++) { + sum += array[i]; + } + return sum; +} // Iteration #3.1 Bonus: -function sum() {} +function sum(array) { + let sum = 0; + for (let i = 0; i < array.length; i++) { + const elementType = typeof array[i]; + if (elementType !== "number" && elementType !== "string" && elementType !== "boolean") { + throw new Error("This element is not valid, it must be a number, a string or a boolean."); + } + else if (array.length === 0) { + return null; + } + else if (typeof array[i] === 'string') { + sum += array[i].length; + } + else if (typeof array[i] === 'boolean') { + if (array[i] === true) { + sum += 1; + } + else { + sum += 0; + } + } else if (typeof array[i] === 'number') { + sum += array[i]; + } else if (typeof array[i] === 'object') { + sum += array[i].length + } + } + return sum; +} @@ -26,16 +80,55 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(array) { + if (array.length === 0) { + return null; + } else { + let sum = sumNumbers(array); + return sum / array.length; + } +} // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(array) { + if (array.length === 0) { + return null; + } + else { + let sum = 0; + for(let i = 0; i < array.length; i++) { + sum += array[i].length; + } + return sum / array.length; + } +} // Bonus - Iteration #4.1 -function avg() {} +function avg(array) { + if (array.length === 0) { + return null; + } + let sum = 0; + for (let i = 0; i < array.length; i++) { + if (typeof array[i] === 'boolean') { + if (array[i] === true) { + sum += 1; + } else if (array[i] === false) { + sum += 0; + } + } else if (typeof array[i] === 'string') { + sum += array[i].length; + } + else if (typeof array[i] === "number") { + sum += array[i]; + } + } + return sum / array.length; +} + // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,14 +145,69 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(array) { + let nonRepitedWords = []; + nonRepitedWords.push(array[0]); + if (array.length === 0) { + return null; + } + else { + for (let i = 1; i < array.length; i++) { + if (!nonRepitedWords.includes(array[i])) { + nonRepitedWords.push(array[i]); + } + } + } + return nonRepitedWords; +} + +/* Another solution: +function uniquifyArray(array) { + let nonRepitedWords = []; + nonRepitedWords.push(array[0]); + if (array.length === 0) { + return null; + } + else { + for (let i = 1; i < array.length; i++) { + if (nonRepitedWords.indexOf(array[i]) == -1) { + nonRepitedWords.push(array[i]); + } + } + } + return nonRepitedWords; +} */ + +/*!nonRepitedWords.includes(array[i])) { + nonRepitedWords += array[i]; + } + } + return nonRepitedWords; + } + +}*/ // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(array, word) { + let checkedWord = 0; + if (array.length === 0) { + return null; + } + for(i = 0; i < array.length; i++) { + if (array[i] === word) { + checkedWord = true; + return checkedWord; + } + else { + checkedWord = false; + } + } + return checkedWord; +} @@ -78,8 +226,15 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} - +function howManyTimes(array, word) { + let count = 0; + for (let i = 0; i < array.length; i++) { + if(word === array[i]) { + count += 1; + } + } + return count; +} // Iteration #8: Bonus