From 86497a749837deb34ae7d8e24b9b82235dc29783 Mon Sep 17 00:00:00 2001 From: A4Revolution <91364746+AdeDeepFishing@users.noreply.github.com> Date: Fri, 9 May 2025 20:09:12 +0900 Subject: [PATCH 1/4] finish 1-3.1 iterations --- src/functions-and-arrays.js | 60 +++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec..52effc4 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,25 +1,73 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - +// takes two numbers as arguments and returns the largest +function maxOfTwoNumbers(num1, num2) { + if (num1 >= num2) { + return num1; + } else{ + return num2; + } +} // Iteration #2: Find longest word +// takes as an argument an array of words and returns the longest one. If there are 2 with the same length, it should return the first occurrence. const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(wordsArr) { + if (wordsArr.length===0){ + return null; + } + let longestWord = wordsArr[0]; + for (let i=1;ilongestWord.length){ + longestWord = wordsArr[i]; + } + } + return longestWord; +} // Iteration #3: Calculate the sum +// iterating over an array and adding each of the elements together. const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(numbersArr) { + if (numbersArr.length===0){ + return 0; + } + let sum=0; + for (let i=0;i Date: Fri, 9 May 2025 20:30:57 +0900 Subject: [PATCH 2/4] finish 4 & 5 --- src/functions-and-arrays.js | 46 +++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 52effc4..e2188ae 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -69,21 +69,42 @@ function sum(mixedArr) { } console.log(sum(mixedArr)); // 57 - // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(numbersArr) { + sumNum=sumNumbers(numbersArr); + return sumNum/numbersArr.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 sum=0; + for (let i=0;i Date: Fri, 9 May 2025 20:34:28 +0900 Subject: [PATCH 3/4] finish 6 & 7 --- src/functions-and-arrays.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index e2188ae..ee23bf4 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -139,8 +139,19 @@ console.log(uniquifyArray(wordsUnique)); // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} - +//take in an array of words as one argument, and a word to search for as the other. Return true if it exists, otherwise, return false. Don't use indexOf for this one. +function doesWordExist(word, wordsFindArr) { + if (wordsFindArr.length===0){ + return null; + } + for (let i=0;i Date: Fri, 9 May 2025 20:46:00 +0900 Subject: [PATCH 4/4] finish 8 & 8.1, all done --- src/functions-and-arrays.js | 49 ++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index ee23bf4..c30a17a 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -208,10 +208,53 @@ const matrix = [ [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] ]; -function greatestProduct() {} - - +// What is the greatest product of four adjacent numbers? We consider adjacent any four numbers that are next to each other horizontally or vertically. +function greatestProduct(matrix) { + let maxProduct = 0; + for (let i = 0; i < matrix.length; i++) { + for (let j = 0; j < matrix[i].length; j++) { + if (j + 3 < matrix[i].length) { + let product = matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3]; + if (product > maxProduct) { + maxProduct = product; + } + } + if (i + 3 < matrix.length) { + let product = matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j]; + if (product > maxProduct) { + maxProduct = product; + } + } + } + } + return maxProduct; +} +console.log(greatestProduct(matrix)); // 51267216 + +//Bonus - Iteration #8.1: Product of diagonals +// It takes a matrix as a parameter and returns the greatest product of any four values layed out diagonally, in either direction. +function greatestProductOfDiagonals(matrix){ + let maxProduct = 0; + for (let i = 0; i < matrix.length; i++) { + for (let j = 0; j < matrix[i].length; j++) { + if (i + 3 < matrix.length && j + 3 < matrix[i].length) { + let product = matrix[i][j] * matrix[i + 1][j + 1] * matrix[i + 2][j + 2] * matrix[i + 3][j + 3]; + if (product > maxProduct) { + maxProduct = product; + } + } + if (i - 3 >= 0 && j + 3 < matrix[i].length) { + let product = matrix[i][j] * matrix[i - 1][j + 1] * matrix[i - 2][j + 2] * matrix[i - 3][j + 3]; + if (product > maxProduct) { + maxProduct = product; + } + } + } + } + return maxProduct; +} +console.log(greatestProductOfDiagonals(matrix)); // 70600674 // The following is required to make unit tests work. /* Environment setup. Do not modify the below code. */