From 7b71ae6d15dbb3ece48394196485e38175716226 Mon Sep 17 00:00:00 2001 From: Amir Mansour Date: Thu, 19 Oct 2023 19:47:23 +0200 Subject: [PATCH] lab done --- src/functions-and-arrays.js | 95 +++++++++++++++++++++++++++++++++---- 1 file changed, 85 insertions(+), 10 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..c4089c571 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,20 +1,45 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(num1, num2) { + 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(wordsArr){ + if(!wordsArr.length) return null + let longestWord = "" + wordsArr.forEach(function(word) { + 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(numArr){ + if(!numArr.length){ + return 0 + } + let sum = 0; + for (let i = 0; i < numArr.length; i++) { + sum += numArr[i]; + } + return sum; +} // Iteration #3.1 Bonus: @@ -26,13 +51,35 @@ 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 null + var sum = 0; + + + numbers.forEach(function (item, idx) { + sum += item; + }); + + + return sum / numbers.length; + } // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(str) { + if(!str.length) + return null + let total = 0; + for(let i = 0; i < str.length; i++){ + total+=str[i].length; + } + let average = total/str.length; + return average + +} // Bonus - Iteration #4.1 function avg() {} @@ -52,16 +99,33 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(arr){ + if(arr.length == 0) + return null + + let myArr = []; +arr.forEach((subject) => { + if (!myArr.includes(subject)) { + myArr.push(subject); + } + +}) +return myArr +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(wordArr, searchWord) { + if(!wordArr.length) + {return null} + else { + return wordArr.includes(searchWord)} +} // Iteration #7: Count repetition const wordsCount = [ @@ -78,7 +142,18 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(wordArray, searchWord) { + { + let count = 0; + + for (let i = 0; i < wordArray.length; i++) { + if (wordArray[i] === searchWord) { + count++; + } + } + + return count; +}}