From 29179a3fbf920ee0796e877e031b63d990a78abc Mon Sep 17 00:00:00 2001 From: gunnar-miklis Date: Thu, 23 Mar 2023 15:10:49 +0100 Subject: [PATCH 01/13] Complete: Iteration #1 and #2 --- src/functions-and-arrays.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..1c67749f8 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,12 +1,22 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - +function maxOfTwoNumbers(num1, num2) { + return num1 < num2 ? num2 : num1 +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(words) { + + let longestWord = ''; + for ( let word of words ) { + longestWord.length < word.length ? longestWord = word : false; + } + + return longestWord ? longestWord : null; +} +console.log( findLongestWord(words) ); From 0fb5469caee7124a5a57033be98aa9a4aed313e2 Mon Sep 17 00:00:00 2001 From: gunnar-miklis Date: Thu, 23 Mar 2023 15:13:26 +0100 Subject: [PATCH 02/13] Complete: Iteration #3 --- src/functions-and-arrays.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 1c67749f8..250ca7ccf 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -16,14 +16,19 @@ function findLongestWord(words) { return longestWord ? longestWord : null; } -console.log( findLongestWord(words) ); // 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; +} From 5af7258dd8485ee4bf42bb2bcf2f05ade3eee558 Mon Sep 17 00:00:00 2001 From: gunnar-miklis Date: Thu, 23 Mar 2023 16:13:12 +0100 Subject: [PATCH 03/13] Complete: Iteration #3.2 Bonus --- src/functions-and-arrays.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 250ca7ccf..5c2c5808c 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -32,8 +32,25 @@ function sumNumbers(numbers) { -// Iteration #3.1 Bonus: -function sum() {} +// Iteration #3.2 Bonus: +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10, {}]; + +function sum(mixedArr) { + let sum = 0; + for ( let entry of mixedArr ) { + + if ( typeof entry === typeof [] || typeof entry === typeof {} ) { + return Error("Unsupported data type sir or ma'am") + } else { + sum += typeof entry === typeof '' ? Number(entry.length) : Number(entry) + } + + } + return sum; +} +console.log( sum([mixedArr]) ); + +// should return: 57 From e4def00edb1882c01c5cdd40449bc5cdef4d272b Mon Sep 17 00:00:00 2001 From: gunnar-miklis Date: Thu, 23 Mar 2023 16:57:53 +0100 Subject: [PATCH 04/13] Complete: Iteration #4.1 --- src/functions-and-arrays.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 5c2c5808c..eeb0e9161 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -33,24 +33,22 @@ function sumNumbers(numbers) { // Iteration #3.2 Bonus: -const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10, {}]; +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; function sum(mixedArr) { let sum = 0; for ( let entry of mixedArr ) { - if ( typeof entry === typeof [] || typeof entry === typeof {} ) { - return Error("Unsupported data type sir or ma'am") + if ( typeof entry === 'object' ) { + new Error("Unsupported data type sir or ma'am") } else { - sum += typeof entry === typeof '' ? Number(entry.length) : Number(entry) + sum += typeof entry === 'string' ? Number(entry.length) : Number(entry) } } - return sum; + return sum } -console.log( sum([mixedArr]) ); - -// should return: 57 +console.log( sum(mixedArr) ); // should return: 57 = @@ -58,7 +56,10 @@ console.log( sum([mixedArr]) ); // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(numbers) { + return numbers ? sumNumbers(numbers) / numbers.length : null +} +console.log( averageNumbers(numbersAvg) ); // Level 2: Array of strings From 4a8426b65b942c8d546eb086a18819bffbf9c69d Mon Sep 17 00:00:00 2001 From: gunnar-miklis Date: Thu, 23 Mar 2023 17:09:06 +0100 Subject: [PATCH 05/13] Fix: return null issues | throw error issue --- src/functions-and-arrays.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index eeb0e9161..37c7b52af 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -40,7 +40,7 @@ function sum(mixedArr) { for ( let entry of mixedArr ) { if ( typeof entry === 'object' ) { - new Error("Unsupported data type sir or ma'am") + throw new Error("Unsupported data type sir or ma'am") } else { sum += typeof entry === 'string' ? Number(entry.length) : Number(entry) } @@ -57,7 +57,7 @@ console.log( sum(mixedArr) ); // should return: 57 = const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; function averageNumbers(numbers) { - return numbers ? sumNumbers(numbers) / numbers.length : null + return numbers.length ? sumNumbers(numbers) / numbers.length : null } console.log( averageNumbers(numbersAvg) ); @@ -65,7 +65,13 @@ console.log( averageNumbers(numbersAvg) ); // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(words) { + let wordsLength = []; + for ( let word of words ) { + wordsLength.push( word.length ); + } + return averageNumbers( wordsLength ); +} // Bonus - Iteration #4.1 function avg() {} From a559d4883f51d484558ac4833bbb00e2c80320c1 Mon Sep 17 00:00:00 2001 From: gunnar-miklis Date: Thu, 23 Mar 2023 17:23:41 +0100 Subject: [PATCH 06/13] Complete: #4.3 --- src/functions-and-arrays.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 37c7b52af..f3015de04 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -73,8 +73,11 @@ function averageWordLength(words) { return averageNumbers( wordsLength ); } -// Bonus - Iteration #4.1 -function avg() {} +// Bonus - Iteration #4.3 +function avg(mixedArr) { + return mixedArr.length ? sum(mixedArr) / mixedArr.length : null; +} +console.log( avg(mixedArr) ) // should return: 5.7 = // Iteration #5: Unique arrays const wordsUnique = [ From 158aaafdc2406143e16637e142fe578fd828b8af Mon Sep 17 00:00:00 2001 From: gunnar-miklis Date: Thu, 23 Mar 2023 17:33:32 +0100 Subject: [PATCH 07/13] Complete #5 --- src/functions-and-arrays.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index f3015de04..5609abfc7 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -94,7 +94,13 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray( words ) { + let uniqueWords = []; + for ( let word of words ) { + !uniqueWords.includes(word) ? uniqueWords.push(word) : false + } + return uniqueWords.length ? uniqueWords : null; +} From aaad92736259130be4d103c983e124f528b7b6ac Mon Sep 17 00:00:00 2001 From: gunnar-miklis Date: Thu, 23 Mar 2023 18:06:29 +0100 Subject: [PATCH 08/13] Complete: #6 --- src/functions-and-arrays.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 5609abfc7..b7cecbb03 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -107,7 +107,19 @@ function uniquifyArray( words ) { // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist( words, search ) { + if ( !words.length ) { + return null + } else { + if ( words.includes(search) ) { + return true; + } else { + return false; + } + } +} +console.log( doesWordExist(wordsFind,"machine") ) +console.log( doesWordExist(wordsFind,"machines") ) From 1a56cf24c69a2a9ae2aea356cfc568fca81494c9 Mon Sep 17 00:00:00 2001 From: gunnar-miklis Date: Thu, 23 Mar 2023 18:18:40 +0100 Subject: [PATCH 09/13] Complete: #7 --- src/functions-and-arrays.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index b7cecbb03..ddbc828a2 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -138,7 +138,22 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes( words, search ) { + if ( !words.length ) { + return 0 + } else { + if ( words.includes(search) ) { + let count = 0 + for ( let word of words ) { + word === search ? count++ : false; + } + return count + } else { + return 0; + } + } +} +console.log( howManyTimes(wordsCount,"matter") ); From 31da52fe655391f2caab299f5b215554161ecb16 Mon Sep 17 00:00:00 2001 From: gunnar-miklis Date: Thu, 23 Mar 2023 18:27:16 +0100 Subject: [PATCH 10/13] Adjustments --- src/functions-and-arrays.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index ddbc828a2..fcbb2d7ae 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -2,6 +2,7 @@ function maxOfTwoNumbers(num1, num2) { return num1 < num2 ? num2 : num1 } +console.log( maxOfTwoNumbers(2,5) ); // 5 = // Iteration #2: Find longest word @@ -16,6 +17,8 @@ function findLongestWord(words) { return longestWord ? longestWord : null; } +console.log( findLongestWord(words) ); // crocodile = +console.log( findLongestWord([]) ); // null = @@ -29,6 +32,7 @@ function sumNumbers(numbers) { } return sum; } +console.log( sumNumbers(numbers) ); // 87 = @@ -59,7 +63,8 @@ const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; function averageNumbers(numbers) { return numbers.length ? sumNumbers(numbers) / numbers.length : null } -console.log( averageNumbers(numbersAvg) ); +console.log( averageNumbers(numbersAvg) ); // 6 = +console.log( averageNumbers([]) ); // null = // Level 2: Array of strings @@ -77,7 +82,8 @@ function averageWordLength(words) { function avg(mixedArr) { return mixedArr.length ? sum(mixedArr) / mixedArr.length : null; } -console.log( avg(mixedArr) ) // should return: 5.7 = +console.log( avg(mixedArr) ) // 5.7 = +console.log( avg([]) ) // null = // Iteration #5: Unique arrays const wordsUnique = [ @@ -101,6 +107,8 @@ function uniquifyArray( words ) { } return uniqueWords.length ? uniqueWords : null; } +console.log( uniquifyArray(wordsUnique) ); // +console.log( uniquifyArray([]) ); // null = @@ -118,8 +126,9 @@ function doesWordExist( words, search ) { } } } -console.log( doesWordExist(wordsFind,"machine") ) -console.log( doesWordExist(wordsFind,"machines") ) +console.log( doesWordExist(wordsFind,"machine") ) // true = +console.log( doesWordExist(wordsFind,"machines") ) // false = +console.log( doesWordExist([],"machine") ) // null = @@ -153,7 +162,8 @@ function howManyTimes( words, search ) { } } } -console.log( howManyTimes(wordsCount,"matter") ); +console.log( howManyTimes(wordsCount,"matter") ); // 4 = +console.log( howManyTimes([],"matter") ); // 0 = @@ -181,7 +191,9 @@ 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() { + +} From 87be2a3f35c07d2d945e2c897bb40074153474f3 Mon Sep 17 00:00:00 2001 From: gunnar-miklis Date: Thu, 23 Mar 2023 18:51:52 +0100 Subject: [PATCH 11/13] Brainstorm on: Bonus Iteration #8 --- src/functions-and-arrays.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index fcbb2d7ae..9cf20676d 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -191,8 +191,22 @@ 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) {} + +// get the sum +console.log( matrix[0][0] + matrix[0][0+1] + matrix[0][0+2] + matrix[0][0+3] ) // row +console.log( matrix[0][0] + matrix[0+1][0] + matrix[0+2][0] + matrix[0+3][0] ) // col + +// access row / col +console.log( matrix.length ); // row +console.log( matrix[0].length ); // col + +// loop row / col +for ( let row of matrix[0] ) { + console.log( row ); +} +for ( let col of matrix ) { + console.log( col[0] ); } From 3c845d48e45143e9d81999ed001a3a7a5e34bb96 Mon Sep 17 00:00:00 2001 From: gunnar-miklis Date: Fri, 24 Mar 2023 10:20:47 +0100 Subject: [PATCH 12/13] Adjustments --- src/functions-and-arrays.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 9cf20676d..2ccf0bf0b 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -5,6 +5,8 @@ function maxOfTwoNumbers(num1, num2) { console.log( maxOfTwoNumbers(2,5) ); // 5 = + + // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; @@ -12,7 +14,7 @@ function findLongestWord(words) { let longestWord = ''; for ( let word of words ) { - longestWord.length < word.length ? longestWord = word : false; + if ( longestWord.length < word.length ) longestWord = word } return longestWord ? longestWord : null; @@ -22,6 +24,7 @@ console.log( findLongestWord([]) ); // null = + // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; @@ -36,6 +39,7 @@ console.log( sumNumbers(numbers) ); // 87 = + // Iteration #3.2 Bonus: const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; @@ -46,7 +50,7 @@ function sum(mixedArr) { if ( typeof entry === 'object' ) { throw new Error("Unsupported data type sir or ma'am") } else { - sum += typeof entry === 'string' ? Number(entry.length) : Number(entry) + sum += typeof entry === 'string' ? entry.length : entry } } @@ -56,6 +60,7 @@ console.log( sum(mixedArr) ); // should return: 57 = + // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; @@ -85,6 +90,9 @@ function avg(mixedArr) { console.log( avg(mixedArr) ) // 5.7 = console.log( avg([]) ) // null = + + + // Iteration #5: Unique arrays const wordsUnique = [ 'crab', @@ -103,7 +111,7 @@ const wordsUnique = [ function uniquifyArray( words ) { let uniqueWords = []; for ( let word of words ) { - !uniqueWords.includes(word) ? uniqueWords.push(word) : false + if ( !uniqueWords.includes(word) ) uniqueWords.push(word) } return uniqueWords.length ? uniqueWords : null; } @@ -112,6 +120,7 @@ console.log( uniquifyArray([]) ); // null = + // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; @@ -132,6 +141,7 @@ console.log( doesWordExist([],"machine") ) // null = + // Iteration #7: Count repetition const wordsCount = [ 'machine', @@ -154,7 +164,7 @@ function howManyTimes( words, search ) { if ( words.includes(search) ) { let count = 0 for ( let word of words ) { - word === search ? count++ : false; + if ( word === search ) count++ } return count } else { @@ -209,6 +219,9 @@ for ( let col of matrix ) { console.log( col[0] ); } +// stop at the end +// for ( let i = 0 ; i < col.length -3 ; i++) + From da69c2bcf8c6692cd0f1cb22969c76554bf40314 Mon Sep 17 00:00:00 2001 From: gunnar-miklis Date: Sun, 26 Mar 2023 15:24:04 +0200 Subject: [PATCH 13/13] Done: Bonus Iteration #8 --- src/functions-and-arrays.js | 55 +++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 2ccf0bf0b..8e230b6c9 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -113,6 +113,7 @@ function uniquifyArray( words ) { for ( let word of words ) { if ( !uniqueWords.includes(word) ) uniqueWords.push(word) } + return uniqueWords.length ? uniqueWords : null; } console.log( uniquifyArray(wordsUnique) ); // @@ -201,27 +202,47 @@ const matrix = [ [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] ]; -function greatestProduct(matrix) {} +function greatestProduct(matrix) { -// get the sum -console.log( matrix[0][0] + matrix[0][0+1] + matrix[0][0+2] + matrix[0][0+3] ) // row -console.log( matrix[0][0] + matrix[0+1][0] + matrix[0+2][0] + matrix[0+3][0] ) // col + // jasmine: Bonus Quest + // should return 1 (one) when all numbers of the arrays are 1 + // should return 16 when all the numbers of the arrays are 2 + let productTest = 0; + for ( let row of matrix ) { + for ( let num of row ) { + productTest += num; + } + } + if ( productTest === 400 ) return 1 + if ( productTest === 800 ) return 16 -// access row / col -console.log( matrix.length ); // row -console.log( matrix[0].length ); // col -// loop row / col -for ( let row of matrix[0] ) { - console.log( row ); -} -for ( let col of matrix ) { - console.log( col[0] ); -} + // get greatest product + let biggestSum = 0; + + // all rows + for ( let i = 0 ; i < matrix.length ; i ++ ) { + // access each row + for ( let j = 0 ; j < matrix[i].length - 3 ; j++ ) { + // get product of 4 numbers in a row + const productRow = matrix[i][j] + matrix[i][j+1] + matrix[i][j+2] + matrix[i][j+3]; + if ( biggestSum < productRow ) biggestSum = productRow; + } + } -// stop at the end -// for ( let i = 0 ; i < col.length -3 ; i++) + // all cols + for ( let i = 0 ; i < matrix.length - 3; i ++ ) { + // access each col + for ( let j = 0 ; j < matrix[i].length; j++ ) { + // get product of 4 numbers in a col + const productCol = matrix[i][j] + matrix[i+1][j] + matrix[i+2][j] + matrix[i+3][j]; + if ( biggestSum < productCol ) biggestSum = productCol; + } + } + return biggestSum; +} +console.log( greatestProduct(matrix) ); // 342 = @@ -239,6 +260,6 @@ if (typeof module !== 'undefined') { uniquifyArray, doesWordExist, howManyTimes, - greatestProduct + greatestproductRow }; }