From b7d5c2cdece50add860429243384f8d05c6677a9 Mon Sep 17 00:00:00 2001 From: mkbcn Date: Sat, 21 Sep 2024 14:52:53 +0200 Subject: [PATCH] Update functions-and-arrays.js --- src/functions-and-arrays.js | 104 ++++++++++++++++++++++++++++++++---- 1 file changed, 93 insertions(+), 11 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..6ccf1f0ff 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,24 +1,56 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(num1, num2) { + + if (num1 > num2) { + return num1; + + } else { + return num2; + + } +} // 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; + } + else if (words.length === 1) { + return words[0]; + } else + return words.sort((one, two) => two.length - one.length)[0]; + +} // 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; + } + else { + let sum = 0; + for (let zahl of numbers) { + sum += zahl; + } + return sum; + + + } + +} // Iteration #3.1 Bonus: -function sum() {} +function sum() { } @@ -26,16 +58,36 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(numbers) { + if (numbers.length) { + return sumNumbers(numbers) / numbers.length; + } else { + return null; + } +} + + + // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(stringArray) { + if (stringArray.length) { + sum = 0; + for (let string of stringArray) { + sum += string.length; + } + return parseInt(sum / stringArray.length); + } else { + return null; + } +} + // Bonus - Iteration #4.1 -function avg() {} +function avg() { } // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,14 +104,28 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(array) { + if (array.length) { + let pushArray = []; + for (let word of array) { + if (!pushArray.includes(word)) { + pushArray.push(word); + } + } return pushArray; + } else { + return null; + } +} + // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(wordsFind, search) { + return (wordsFind.length) ? wordsFind.includes(search) : null; +} @@ -78,7 +144,23 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(wordsCount, search) { + if (wordsCount.length) { + let counter = 0; + for (let word of wordsCount) { + if (word === search) { + console.log(word); + counter++; + } else { + continue; + } + } + return counter; + } else { + return 0; + } +} + @@ -106,7 +188,7 @@ const matrix = [ [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] ]; -function greatestProduct() {} +function greatestProduct() { }