From 80521981a8492850b016b7955ef10d32114ea512 Mon Sep 17 00:00:00 2001 From: Eyob Zewde Date: Sat, 25 Jan 2025 13:34:23 +0900 Subject: [PATCH] solved lab --- src/functions-and-arrays.js | 160 +++++++++++++++++++++++++++++++++--- 1 file changed, 148 insertions(+), 12 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec..4418efa 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,41 +1,125 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(num1,num2) { + if (num1>num2){ + return(num1+ " is greater") + } + else if (num1longest.length){ + longest=arr[i] + } + + } + return longest + + +} +console.log(findLongestWord(empty)) +console.log(findLongestWord(words)) -function findLongestWord() {} // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(arr) { + let sum=0 + for(let i=0; i acc + curr, 0); + return sum / arr.length; +} +console.log(averageNumbers(number)); // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(arr) { + if (arr.length === 0) return null; + const totalLength = arr.reduce((acc, curr) => acc + curr.length, 0); + return totalLength / arr.length; + } + console.log(averageWordLength(words)); // Bonus - Iteration #4.1 -function avg() {} +function avg(arr) { + if (arr.length === 0) return null; + const total = arr.reduce((acc, curr) => { + if (typeof curr === "number") { + return acc + curr; + } else if (typeof curr === "string") { + return acc + curr.length; + } else if (typeof curr === "boolean") { + return acc + (curr ? 1 : 0); + } else { + throw new Error("Unsupported data type in array"); + } + }, 0); + + return total / arr.length; +} +console.log(avg(mixedArr)); + // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,14 +136,27 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(arr) { + if (arr.length === 0) return null; + return arr.filter((word, index) => arr.indexOf(word) === index); +} +console.log(uniquifyArray(words)); + // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(arr,word) { + if (arr.length === 0) return null; + for (let i = 0; i < arr.length; i++) { + if (arr[i] === word) return true; + } + return false; // Word does not exist +} +console.log(doesWordExist(words, 'truth')); +console.log(doesWordExist(words, 'hello')); @@ -78,7 +175,17 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(arr,word) { + if (arr.length === 0) return 0; + let count = 0; + for (let i = 0; i < arr.length; i++) { + if (arr[i] === word) count++; + } + return count; +} +console.log(howManyTimes(words, 'matter')); +console.log(howManyTimes(words, 'truth')); + @@ -106,7 +213,36 @@ 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(matrix) { + let maxProduct = 0; + + for (let row = 0; row < matrix.length; row++) { + for (let col = 0; col < matrix[row].length; col++) { + + if (col + 3 < matrix[row].length) { + const horizontalProduct = + matrix[row][col] * + matrix[row][col + 1] * + matrix[row][col + 2] * + matrix[row][col + 3]; + maxProduct = Math.max(maxProduct, horizontalProduct); + } + + if (row + 3 < matrix.length) { + const verticalProduct = + matrix[row][col] * + matrix[row + 1][col] * + matrix[row + 2][col] * + matrix[row + 3][col]; + maxProduct = Math.max(maxProduct, verticalProduct); + } + } + } + + return maxProduct; +} +console.log(greatestProduct(matrix)); +