From 6ae1cf112c89b067e47dcf38196dc80fb0a52218 Mon Sep 17 00:00:00 2001 From: premkumardev909 Date: Sat, 19 Oct 2024 03:13:44 +0000 Subject: [PATCH 1/2] Solved lab --- src/functions-and-arrays.js | 77 +++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 11 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec..da8fb0f 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,24 +1,53 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(a, b) { + return a > b ? a : b; +} // 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 longestWord = words[0]; + for (let word of words) { + 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(numbers) { + let sum = 0; + for (let number of numbers) { + sum += number; + } + return sum; +} // Iteration #3.1 Bonus: -function sum() {} +function sum(arr) { + let total = 0; + for (let item of arr) { + 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; +} @@ -26,16 +55,23 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} - +function averageNumbers(numbers) { + return sumNumbers(numbers) / numbers.length; +} // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(words) { + let totalLength = sum(words.map(word => word.length)); + return totalLength / words.length; +} // Bonus - Iteration #4.1 -function avg() {} +function avg(arr) { + const total = sum(arr); + return total / arr.length; +} // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,14 +88,27 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(words) { + const uniqueWords = []; + for (let word of words) { + if (!uniqueWords.includes(word)) { + uniqueWords.push(word); + } + } + return uniqueWords; +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(words, wordToSearch) { + for (let word of words) { + if (word === wordToSearch) return true; + } + return false; +} @@ -78,7 +127,13 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(words, wordToCount) { + let count = 0; + for (let word of words) { + if (word === wordToCount) count++; + } + return count; +} From a2d56446ab742fe0a3869c417d1ecf78cce00ae6 Mon Sep 17 00:00:00 2001 From: premkumardev909 Date: Sat, 19 Oct 2024 03:27:50 +0000 Subject: [PATCH 2/2] solved lab --- src/functions-and-arrays.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index da8fb0f..79f50bb 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -44,6 +44,8 @@ function sum(arr) { total += item.length; } else if (typeof item === 'boolean') { total += item ? 1 : 0; + } else { + throw new Error("Unsupported data type sir or ma'am"); } } return total; @@ -56,19 +58,21 @@ function sum(arr) { const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; function averageNumbers(numbers) { + if (numbers.length === 0) return null; // Return null for empty array return sumNumbers(numbers) / numbers.length; } - // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; function averageWordLength(words) { + if (words.length === 0) return null; // Return null for empty array let totalLength = sum(words.map(word => word.length)); return totalLength / words.length; } // Bonus - Iteration #4.1 function avg(arr) { + if (arr.length === 0) return null; // Return null for empty array const total = sum(arr); return total / arr.length; } @@ -89,6 +93,7 @@ const wordsUnique = [ ]; function uniquifyArray(words) { + if (words.length === 0) return null; // Return null for empty array const uniqueWords = []; for (let word of words) { if (!uniqueWords.includes(word)) { @@ -99,11 +104,11 @@ function uniquifyArray(words) { } - // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; function doesWordExist(words, wordToSearch) { + if (words.length === 0) return null; // Return null for empty array for (let word of words) { if (word === wordToSearch) return true; } @@ -111,7 +116,6 @@ function doesWordExist(words, wordToSearch) { } - // Iteration #7: Count repetition const wordsCount = [ 'machine', @@ -161,7 +165,29 @@ 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; + + // Check horizontally and vertically for maximum product of four adjacent numbers. + for (let i = 0; i < matrix.length; i++) { + for (let j = 0; j < matrix[i].length; j++) { + // Horizontal product + if (j < matrix[i].length - 3) { + const horizontalProduct = + matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3]; + maxProduct = Math.max(maxProduct, horizontalProduct); + } + // Vertical product + if (i < matrix.length - 3) { + const verticalProduct = + matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j]; + maxProduct = Math.max(maxProduct, verticalProduct); + } + } + } + + return maxProduct; // Ensure this returns the maximum product found +}