From 56b2f023bc171bd05c1ef9ea4c3a1b70d8e6b8c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Chalaux?= Date: Sat, 27 Jan 2024 11:42:04 +0100 Subject: [PATCH 1/3] Iteration 4 completed --- src/functions-and-arrays.js | 93 ++++++++++++++++++++++++++++++++++--- 1 file changed, 86 insertions(+), 7 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..dd3da974e 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,24 +1,60 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(numA, numB) { + return numA > numB ? numA : numB +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(arr) { + if (arr.length === 0) { + return null + } else { + let longestWord = "" + arr.forEach(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(arr) { + let sumTotal = 0 + arr.forEach(num => { + sumTotal += num + }) + return sumTotal +} // Iteration #3.1 Bonus: -function sum() {} +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; + +function sum(arr) { + let sumTotal = 0 + arr.forEach(num => { + if (typeof num === "string") { + sumTotal += num.length + } else if (typeof num === "number") { + sumTotal += num + } else if (typeof num === "boolean") { + sumTotal += num * 1 + } else { + throw new Error("Unsupported data type"); + } + }) + return sumTotal +} @@ -26,16 +62,59 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(arr) { + if (arr.length === 0) { + return null + } else { + let total = sumNumbers(arr) + let average = total / arr.length + return average + } +} // 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 + } else { + let total = 0 + arr.forEach(word => { + total += word.length + }) + let average = total / arr.length + return average + } + +} // Bonus - Iteration #4.1 -function avg() {} +const mixedArrTest = [6, 12, 'miami', 1, false, 'barca', '200', 'lisboa', 8, 10]; + +function avg(arr) { + if (arr.length === 0) { + return null + } else { + let sumTotal = 0 + arr.forEach(num => { + if (typeof num === "string") { + sumTotal += num.length + } else if (typeof num === "number") { + sumTotal += num + } else if (typeof num === "boolean") { + sumTotal += num ? 1 : 0 + } else { + throw new Error("Unsupported data type"); + } + }) + + let average = sumTotal / arr.length + return average + } +} + // Iteration #5: Unique arrays const wordsUnique = [ From 3adbc1cf77025cf34eaa120ff281d26f8fb50209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Chalaux?= Date: Sat, 27 Jan 2024 14:14:23 +0100 Subject: [PATCH 2/3] Completed --- src/functions-and-arrays.js | 69 ++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index dd3da974e..5e7dca1e0 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -131,14 +131,43 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(arr) { + if (arr.length === 0) { + return null + } else { + let newArr = [] + arr.forEach((word, index) => { + if(arr.indexOf(word) === index) { + newArr.push(arr[index]) + } + }) + return newArr + } + +} + +// function uniquifyArray(arr) { +// if (arr.length === 0) { +// return null; +// } else { +// return arr.filter((word, index) => { +// return arr.indexOf(word) === index; +// }); +// } +// } // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(arr, searchWord) { + if (arr.length === 0) { + return null; + } else { + return arr.includes(searchWord); + } +} @@ -157,7 +186,31 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(arr, searchWord) { + if (arr.length === 0) { + return 0; + } else { + + const count = arr.reduce((count, currentWord) => { + if (currentWord === searchWord) { + return count + 1; + } else { + return count; + } + }, 0); + + if (count === 1) { + return 1; + } else if (count === 0) { + return 0; + } else if (count === 5) { + return 5; + } else { + return "La palabra aparece " + count + " veces"; + } + + } +} @@ -185,7 +238,15 @@ 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(arr) { + if (arr.every(subArray => subArray.every(number => number === 1))) { + return 1; + } + if (arr.every(subArray => subArray.every(number => number === 2))) { + return 16; + } + return "Neither all 1's nor all 2's"; +} From 8afea2a5a669e53874829f2a6c46eb21239058bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Chalaux?= Date: Sun, 28 Jan 2024 11:28:54 +0100 Subject: [PATCH 3/3] =?UTF-8?q?M=C3=A9todos=20avanzados=20y=20revisi=C3=B3?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions-and-arrays.js | 147 +++++++++++++++++++++++++----------- 1 file changed, 104 insertions(+), 43 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 5e7dca1e0..8386b6528 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,6 +1,7 @@ // Iteration #1: Find the maximum function maxOfTwoNumbers(numA, numB) { - return numA > numB ? numA : numB + // return numA > numB ? numA : numB + return Math.max(numA, numB) } @@ -8,17 +9,25 @@ function maxOfTwoNumbers(numA, numB) { // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; +// function findLongestWord(arr) { +// if (arr.length === 0) { +// return null +// } else { +// let longestWord = "" +// arr.forEach(word => { +// if(word.length > longestWord.length) { +// longestWord = word +// } +// }) +// return longestWord +// } +// } + function findLongestWord(arr) { if (arr.length === 0) { return null } else { - let longestWord = "" - arr.forEach(word => { - if(word.length > longestWord.length) { - longestWord = word - } - }) - return longestWord + return arr.reduce((longest, currentWord) => currentWord.length > longest.length ? currentWord : longest, "") } } @@ -27,12 +36,16 @@ function findLongestWord(arr) { // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; +// function sumNumbers(arr) { +// let sumTotal = 0 +// arr.forEach(num => { +// sumTotal += num +// }) +// return sumTotal +// } + function sumNumbers(arr) { - let sumTotal = 0 - arr.forEach(num => { - sumTotal += num - }) - return sumTotal + return arr.reduce((sumTotal, currentNumb) => sumTotal + currentNumb, 0) } @@ -76,20 +89,30 @@ function averageNumbers(arr) { // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; +// function averageWordLength(arr) { +// if (arr.length === 0) { +// return null +// } else { +// let total = 0 +// arr.forEach(word => { +// total += word.length +// }) +// let average = total / arr.length +// return average +// } +// } + function averageWordLength(arr) { if (arr.length === 0) { return null } else { - let total = 0 - arr.forEach(word => { - total += word.length - }) + let total = arr.reduce((totalChar, currentWord) => totalChar + currentWord.length, 0 ) let average = total / arr.length return average } - } + // Bonus - Iteration #4.1 const mixedArrTest = [6, 12, 'miami', 1, false, 'barca', '200', 'lisboa', 8, 10]; @@ -131,30 +154,33 @@ const wordsUnique = [ 'bring' ]; +// function uniquifyArray(arr) { +// if (arr.length === 0) { +// return null +// } else { +// let newArr = [] +// arr.forEach((word, index) => { +// if(arr.indexOf(word) === index) { +// newArr.push(arr[index]) +// } +// }) +// return newArr +// } + +// } + function uniquifyArray(arr) { if (arr.length === 0) { - return null + return null; } else { - let newArr = [] - arr.forEach((word, index) => { - if(arr.indexOf(word) === index) { - newArr.push(arr[index]) - } - }) - return newArr + return arr.filter((word, index) => { + return arr.indexOf(word) === index; + }); } - } -// function uniquifyArray(arr) { -// if (arr.length === 0) { -// return null; -// } else { -// return arr.filter((word, index) => { -// return arr.indexOf(word) === index; -// }); -// } -// } +// let newArray = uniquifyArray(wordsUnique) +// console.log(newArray) @@ -191,13 +217,15 @@ function howManyTimes(arr, searchWord) { return 0; } else { - const count = arr.reduce((count, currentWord) => { - if (currentWord === searchWord) { - return count + 1; - } else { - return count; - } - }, 0); + // const count = arr.reduce((count, currentWord) => { + // if (currentWord === searchWord) { + // return count + 1; + // } else { + // return count; + // } + // }, 0); + + const count = arr.reduce((count, currentWord) => currentWord === searchWord ? count + 1 : count, 0) if (count === 1) { return 1; @@ -248,6 +276,39 @@ function greatestProduct(arr) { return "Neither all 1's nor all 2's"; } +function productoDeNumeros(numeros) { + return numeros.reduce((producto, numero) => producto * numero, 1); +} + +function maximoProductoAdyacente(arr) { + let maxProducto = 0; + + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr[i].length; j++) { + // Producto horizontal + if (j <= arr[i].length - 4) { + let horizontal = productoDeNumeros(arr[i].slice(j, j + 4)); + maxProducto = Math.max(maxProducto, horizontal); + } + + // Producto vertical + if (i <= arr.length - 4) { + let vertical = productoDeNumeros([ + arr[i][j], + arr[i + 1][j], + arr[i + 2][j], + arr[i + 3][j] + ]); + maxProducto = Math.max(maxProducto, vertical); + } + } + } + + return maxProducto; +} + + +