From 414f53fc2ae2afd0c1ef0fb3d9710e012850009e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A8Lucintheskies=C2=A8?= <¨rodriguezluciamtilde@gmail.com¨> Date: Fri, 4 Jul 2025 11:00:39 +0200 Subject: [PATCH 1/4] functions --- README.md | 2 +- src/functions-and-arrays.js | 129 +++++++++++++++++++++++++++++++----- src/index.html | 12 ++++ 3 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 src/index.html diff --git a/README.md b/README.md index ab9fadd9a..db938a1c9 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ lab-js-functions-and-arrays ``` We will be working with the `src/functions-and-arrays.js`. You can find all the files in the `jasmine` folder needed to use Jasmine. All these files are already linked with the `SpecRunner.html` file. - +<> If you want to check the tests, they are in the `tests/functions-and-arrays.spec.js` file. diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..1776bf76d 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,41 +1,85 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(a,b) { + if (a > b) { + return a; + } else if (b > a) { + return b; + } else { + return a; + } +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(word) { + if (word.length === 0) { + return null; + } + + let longestWord = word[0]; + for (let i = 1; i < word.length; i++) { + if (word[i].length > longestWord.length) { + longestWord = word[i]; + } + } + return longestWord; +} -// Iteration #3: Calculate the sum +// Iteration #3: Calculate the sum of array numbers const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(numbers) { +for (let i = 0; i < numbers.length; i++) { +} +} // Iteration #3.1 Bonus: -function sum() {} - - +/*function sum(numbers) { + return numbers.reduce((acc, curr) => acc + curr, 0);*/ -// Iteration #4: Calculate the average +/// Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(numbers) { + if (!numbers || numbers.length === 0) { + return null; + } + const sum = sumNumbers(numbers); + return sum / numbers.length; +} + +/* Level 2: Array of strings +const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; + +function averageWordLength(words) { + if (!words || words.length === 0) { + return null; + } + const totalLength = words.reduce((acc, curr) => acc + curr.length, 0); + return totalLength / words.length; +}*/ // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength() { + +} + // Bonus - Iteration #4.1 -function avg() {} +function avg(total) { + +} // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,14 +96,21 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray() { + +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(word) { + if (!word || word.length === 0) { + return null; + } + return wordsFind.includes(word); +} @@ -78,7 +129,18 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(word) { + if (!word || word.length === 0) { + return 0; + } + let count = 0; + for (let i = 0; i < wordsCount.length; i++) { + if (wordsCount[i] === word) { + count++; + } + } + return count; +} @@ -106,10 +168,41 @@ 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 i = 0; i < matrix.length; i++) { + for (let j = 0; j < matrix[i].length; j++) { + const current = matrix[i][j]; + + // Check right + if (j + 3 < matrix[i].length) { + const product = current * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3]; + maxProduct = Math.max(maxProduct, product); + } + + // Check down + if (i + 3 < matrix.length) { + const product = current * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j]; + maxProduct = Math.max(maxProduct, product); + } + + // Check diagonal right + if (i + 3 < matrix.length && j + 3 < matrix[i].length) { + const product = current * matrix[i + 1][j + 1] * matrix[i + 2][j + 2] * matrix[i + 3][j + 3]; + maxProduct = Math.max(maxProduct, product); + } + + // Check diagonal left + if (i + 3 < matrix.length && j - 3 >= 0) { + const product = current * matrix[i + 1][j - 1] * matrix[i + 2][j - 2] * matrix[i + 3][j - 3]; + maxProduct = Math.max(maxProduct, product); + } + } + } + + return maxProduct; +} // The following is required to make unit tests work. /* Environment setup. Do not modify the below code. */ diff --git a/src/index.html b/src/index.html new file mode 100644 index 000000000..abccf09e7 --- /dev/null +++ b/src/index.html @@ -0,0 +1,12 @@ + + + + + Lab: JavaScript Functions and Arrays + + +

Lab: JavaScript Functions and Arrays

+

Abre la consola para ver los resultados de tu código JavaScript.

+ + + \ No newline at end of file From b83e465f846ce6f0a7495038a24dd3aecb5a78e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A8Lucintheskies=C2=A8?= <¨rodriguezluciamtilde@gmail.com¨> Date: Fri, 4 Jul 2025 12:29:35 +0200 Subject: [PATCH 2/4] functions --- src/functions-and-arrays.js | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 1776bf76d..856edb207 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -32,11 +32,14 @@ function findLongestWord(word) { // Iteration #3: Calculate the sum of array numbers const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; - function sumNumbers(numbers) { -for (let i = 0; i < numbers.length; i++) { + if (!Array.isArray(numbers) || numbers.length === 0) return 0; // guard clause -} + let sum = 0; // accumulator + for (let i = 0; i < numbers.length; i++) { + sum += numbers[i]; // add each element + } + return sum; // give back the total } @@ -69,11 +72,33 @@ function averageWordLength(words) { // Level 2: Array of strings -const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; - -function averageWordLength() { +const wordsArr = [ + 'seat', 'correspond', 'linen', 'motif', + 'hole', 'smell', 'smart', 'chaos', + 'fuel', 'palace' +]; +/** + * Computes the average word length of an array of strings. + * @param {string[]} words – Array of words. + * @returns {number|null} Average length, or null if the array is empty. + */ +function averageWordLength(words = []) { + if (!Array.isArray(words) || words.length === 0) return null; // 1️⃣ + + // 2️⃣ & 3️⃣ – works for one‑element arrays and larger ones + let total = 0; + for (let i = 0; i < words.length; i++) { + total += words[i].length; + } + return total / words.length; } + +// Quick checks +console.log(averageWordLength([])); // → null +console.log(averageWordLength(['hello'])); // → 5 +console.log(averageWordLength(wordsArr)); // → 5.3 + // Bonus - Iteration #4.1 From f56fd02e2afe42b702041c2484928ed637b5be07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A8Lucintheskies=C2=A8?= <¨rodriguezluciamtilde@gmail.com¨> Date: Fri, 4 Jul 2025 12:35:25 +0200 Subject: [PATCH 3/4] functions --- src/functions-and-arrays.js | 39 ++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 856edb207..8a9bad243 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -43,9 +43,42 @@ function sumNumbers(numbers) { } -// Iteration #3.1 Bonus: -/*function sum(numbers) { - return numbers.reduce((acc, curr) => acc + curr, 0);*/ +/** + * Bonus – Iteration #3.1: generic sum() + * Suma números, longitudes de strings y valores de booleanos. + * - number → su valor + * - string → length + * - boolean → 1 si es true, 0 si es false + * Lanza un error si aparece cualquier otro tipo. + * + * @param {Array} arr + * @returns {number} total de la suma (0 si el array está vacío) + */ +function sum(arr = []) { + if (!Array.isArray(arr) || arr.length === 0) return 0; // “empty array” ⇒ 0 + + let total = 0; + + for (const el of arr) { + switch (typeof el) { + case 'number': + total += el; + break; + case 'string': + total += el.length; + break; + case 'boolean': + total += el ? 1 : 0; + break; + default: + throw new Error( + `Unsupported data type (${typeof el}) found in array – only numbers, strings and booleans are allowed.` + ); + } + } + + return total; +} /// Iteration #4: Calculate the average // Level 1: Array of numbers From eb3ddd1c6c0575e7f0ec2d3e5432ea4dc259109c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A8Lucintheskies=C2=A8?= <¨rodriguezluciamtilde@gmail.com¨> Date: Fri, 4 Jul 2025 14:41:10 +0200 Subject: [PATCH 4/4] functions --- src/functions-and-arrays.js | 45 +++++-------------------------------- 1 file changed, 5 insertions(+), 40 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 8a9bad243..921b576e6 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -43,42 +43,9 @@ function sumNumbers(numbers) { } -/** - * Bonus – Iteration #3.1: generic sum() - * Suma números, longitudes de strings y valores de booleanos. - * - number → su valor - * - string → length - * - boolean → 1 si es true, 0 si es false - * Lanza un error si aparece cualquier otro tipo. - * - * @param {Array} arr - * @returns {number} total de la suma (0 si el array está vacío) - */ -function sum(arr = []) { - if (!Array.isArray(arr) || arr.length === 0) return 0; // “empty array” ⇒ 0 - - let total = 0; - - for (const el of arr) { - switch (typeof el) { - case 'number': - total += el; - break; - case 'string': - total += el.length; - break; - case 'boolean': - total += el ? 1 : 0; - break; - default: - throw new Error( - `Unsupported data type (${typeof el}) found in array – only numbers, strings and booleans are allowed.` - ); - } - } - - return total; -} +// Iteration #3.1 Bonus: +/*function sum(numbers) { + return numbers.reduce((acc, curr) => acc + curr, 0);*/ /// Iteration #4: Calculate the average // Level 1: Array of numbers @@ -167,8 +134,7 @@ function doesWordExist(word) { if (!word || word.length === 0) { return null; } - return wordsFind.includes(word); -} + @@ -277,5 +243,4 @@ if (typeof module !== 'undefined') { doesWordExist, howManyTimes, greatestProduct - }; -} + }