diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..1f674ba96 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,24 +1,76 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(num1, num2) { + if (num1 > num2) { + return num1; + } else if (num1 < num2) { + return num2; + } else { + return num1; + } +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(words) { + if (words.length === 0) { + return null; + } + let longest = words[0]; + + for (let i = 1; i < words.length; i++) { + if (words[i].length > longest.length) { + longest = words[i]; + } + } + + return longest; +} // 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 sum = 0; + + for (let i = 0; i < numbers.length; i++) { + sum += numbers[i] + } + return sum +} // Iteration #3.1 Bonus: -function sum() {} +function sum(numbers) { + if (!numbers.length) return 0; + + let total = 0; + + for (let item of numbers) { + if (typeof item === 'number') { + total += item; + } else if (typeof item === 'string') { + total += item.length; + } else if (typeof item === 'boolean') { + total += item ? 1 : 0; + } else { + throw new Error("no data type availbale") + } + } + + return total; +} + + @@ -26,16 +78,50 @@ function sum() {} // 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 total = 0; + + for (let num of numbersAvg) { + total += num; + } + return total / numbersAvg.length; +} // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(wordsArr) { + if ( wordsArr.length === 0) return null; + + let average = 0; + + for ( let word of wordsArr) { + average += word.length; + } + return average/wordsArr.length; + } // Bonus - Iteration #4.1 -function avg() {} +function avg(wordsArr) { + if (wordsArr.length === 0) return null; + + let total = 0; + + for (let item of wordsArr) { + if (typeof item === 'number') { + total += item; + } else if (typeof item === 'string') { + total += item.length; + } else if (typeof item === 'boolean') { + total += item ? 1 : 0; + } + } + return total/wordsArr.length; +} + // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,15 +138,29 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(wordsUnique) { + if (wordsUnique.length === 0) return null; + +let newWordsUnique = []; + +for (let word of wordsUnique) { + if(!newWordsUnique.includes(word)) { + newWordsUnique.push(word); + } +} +return newWordsUnique; +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(wordsFind, wordToSearch) { + if (wordsFind.length === 0) return null; + return wordsFind.includes(wordToSearch); +} // Iteration #7: Count repetition @@ -78,7 +178,18 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(wordsCount, wordToCount) { + if (wordsCount.length === 0) return 0; + + let count = 0; + + for (let word of wordsCount) { + if (word === wordToCount) { + count++; + } + } + return count; +}