From c23982ef95e615f2db20706be6ce694ee23460a4 Mon Sep 17 00:00:00 2001 From: karlajaramillo Date: Sun, 19 Sep 2021 18:56:04 +0200 Subject: [PATCH 1/6] Add Iteration #1, Iteration #2, Iteration #3, Iteration #4, Iteration #5, Iteration #6, Iteration #7, Bonus Iteration #3.1, Bonus Iteration #4.1 --- src/functions-and-arrays.js | 235 ++++++++++++++++++++++++++++++++---- 1 file changed, 214 insertions(+), 21 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..56c24596c 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,24 +1,109 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - +function maxOfTwoNumbers(a,b) { + if (a > b) { + return a; + } else { + return b; + } +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} - - +function findLongestWord(words) { + let longest = ""; + let number = 0; + let arr = []; + let max; + if(words.length === 0 ) { + return null; + } + + words.forEach( item => { + arr.push(item.length); + }) + // calculate the maximum value of an array + max = Math.max(...arr); + // iterates through 'arr' to find the first maximum length + + for(let i = 0; i < arr.length; i ++) { + // if the item is === max, return the value + if(arr[i]=== max) { + longest = words[i]; + return longest; + } + } +} // 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 i = 0; i < numbers.length; i ++) { + sum += numbers[i]; + } + return sum; +} +// Option 2 +// function sumNumbers(numbers) { +// let counter = 0; +// numbers.forEach(item => { +// counter += item; +// }) +// return counter; +// } + +// Option 3 +// function sumNumbers(numbers) { +// // return the result of reduce +// return numbers.reduce((a,b) => { // Explicit return, in more than 1 line, with {} and 'return' keyword +// return a + b +// },0); +// } + +// Option 3 +// function sumNumbers(numbers) { +// // return the result of reduce +// return numbers.reduce((a,b) => a + b, 0);// Implicit return, in one line without the word 'return' +// } // Iteration #3.1 Bonus: -function sum() {} +//sum() that calculates the sum for array filled with (almost) any type of data. +//Note that strings should have their length added to the total, +//and boolean values should be coerced into their corresponding numeric values. + + +function sum(numbers) { + let counter = 0; + + numbers.forEach(item => { + let notValid = typeof item === 'object' || Array.isArray(item); + let isString = typeof item === 'string'; + let isBoolean = typeof item === 'boolean'; + let isNumber = typeof item === 'number'; + + if (notValid) { + throw `Unsupported data type sir or ma'am`; + } + + if (isNumber) { + counter += item; + } else if (isString) { + counter += item.length; + } else if (isBoolean) { + if (item === false) { + counter += 0; + } else if (item === true) { + counter += 1; + } + } + }) + return counter; +} @@ -26,16 +111,79 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(numbers) { + //should return null if receives an empty array when called + if(numbers.length === 0) { + return null; + } + let totalNumbers = numbers.length; + let average; + let sum = sumNumbers(numbers); + + average = sum / totalNumbers; + return average; +} // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(words) { + let arrWordsLength = []; + + //iterate to create new array with length of words + words.forEach(item => { + arrWordsLength.push(item.length); + }); + // Average of numbers based on the Array of the strings + let avgNumbers = averageNumbers(arrWordsLength); + return avgNumbers; +} + +// function averageWordLength(words) { +// let sum = 0; +// let arrWordsLength = []; +// let totalItems = words.length; +// let totalAverage; + +// //iterate to create new array with length of words +// words.forEach(item => { +// arrWordsLength.push(item.length); +// }); +// //console.log(arrWordsLength); + +// //iterate to sum the length of numbers +// //Option 2 -- forEach loop +// arrWordsLength.forEach(item => { +// sum += item; +// }); + +// //Option 3 -- for loop +// // for(let i = 0; i < arrWordsLength.length; i ++) { +// // counter += arrWordsLength[i]; +// // //console.log(`counter: ${counter}`); +// // } +// totalAverage = sum/totalItems; +// //console.log(totalAverage) +// return totalAverage; +// } + +//const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; +// Expected --> 5.7 +// But there is an error on the tests--> expected 5.1 // Bonus - Iteration #4.1 -function avg() {} +function avg(arr) { + let totalItems = arr.length; + // check if there is an empty array + if(arr.length === 0) { + return null; + } + // Use Function Sum to iterate over the items and sum mixed items + let sumMixedItems = sum(arr); + let avgMixedItems = sumMixedItems/totalItems; + return avgMixedItems; +} // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,17 +200,45 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} - - +//remove the duplicates, and return a new array. +//You are more than likely going to want to check out the indexOf Array method. + +function uniquifyArray(words) { + if(words.length === 0) { + return null; + } + let unique = []; + + words.forEach( word => { + //console.log(`item: ${item}`); + // Checks if the word it's not included inside the array given 'words' + const isWordRepeated = unique.includes(word); + if(!isWordRepeated) {//is not included + unique.push(word); + } + }) + return unique; +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; - -function doesWordExist() {} - - - +//that will 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. +//that will 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. +//Don't use indexOf for this one. +function doesWordExist(arrWords, word) { + // return null if the array is empty + if (arrWords.length === 0) { + return null; + } + // if the word is included into the array --> true + if (arrWords.includes(word)) { + return true; + } else { // if the word is not included inside the array --> false + return false; + } +} // Iteration #7: Count repetition const wordsCount = [ 'machine', @@ -77,8 +253,25 @@ const wordsCount = [ 'disobedience', 'matter' ]; - -function howManyTimes() {} +// Iteration #7: Count repetition +function howManyTimes(arrWords, wordToCheck) { + // if the array is empty, return 0 + if(arrWords.length === 0) { + return 0; + } + let counter = 0; + //iterates to check if the wordToCheck is included inside the array of words + arrWords.forEach(word => { + //let isWordIncluded = word.includes(word); + let isWordIncluded = word === wordToCheck; + if(isWordIncluded) { + counter +=1; + } + }) + return counter; +} +//howManyTimes(wordsCount, "matter") +// 4 @@ -99,7 +292,7 @@ const matrix = [ [86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58], [19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40], [4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66], - [88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69], + [88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 412, 32, 63, 93, 53, 69], [4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36], [20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16], [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54], From d30bb38d04751b1655cab558622a0ad7ab6eb2f6 Mon Sep 17 00:00:00 2001 From: karlajaramillo Date: Sun, 19 Sep 2021 19:39:35 +0200 Subject: [PATCH 2/6] All test passed - except Bonus Iteration 8 --- src/functions-and-arrays.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 56c24596c..5f4f514e8 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -7,7 +7,6 @@ function maxOfTwoNumbers(a,b) { } } - // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; @@ -182,7 +181,8 @@ function avg(arr) { // Use Function Sum to iterate over the items and sum mixed items let sumMixedItems = sum(arr); let avgMixedItems = sumMixedItems/totalItems; - return avgMixedItems; + + return Number(avgMixedItems.toFixed(2)); } // Iteration #5: Unique arrays From bd5d743e2a16ac14488bb977b34729207b5b726a Mon Sep 17 00:00:00 2001 From: karlajaramillo Date: Sun, 19 Sep 2021 23:51:58 +0200 Subject: [PATCH 3/6] Remove console log and add lab instructions --- src/functions-and-arrays.js | 209 ++++++++++++++++++------------------ 1 file changed, 102 insertions(+), 107 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 5f4f514e8..dd7a79ca3 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,4 +1,6 @@ // Iteration #1: Find the maximum +//Implement the function maxOfTwoNumbers that takes two numbers as arguments +//and returns the largest. function maxOfTwoNumbers(a,b) { if (a > b) { return a; @@ -8,55 +10,48 @@ function maxOfTwoNumbers(a,b) { } // Iteration #2: Find longest word -const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; +//Implement the function findLongestWord that 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(words) { - let longest = ""; - let number = 0; - let arr = []; - let max; + let longestWord = ""; // save the longest word with length + // check if the string is empty if(words.length === 0 ) { return null; } - words.forEach( item => { - arr.push(item.length); - }) - // calculate the maximum value of an array - max = Math.max(...arr); - // iterates through 'arr' to find the first maximum length - - for(let i = 0; i < arr.length; i ++) { - // if the item is === max, return the value - if(arr[i]=== max) { - longest = words[i]; - return longest; + words.forEach( word => { + //check if the length of the word is > the maximum length + if(word.length > longestWord.length) { + longestWord = word; // assign/update with the longest word } - } + }) + return longestWord; } // Iteration #3: Calculate the sum +//Implement the function named sumNumbers that takes an array of numbers as an argument, +//and returns the sum of all of the numbers in the array + const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; +// Option 1 - for.. of loop function sumNumbers(numbers) { + // check if it the argument is a valid array + let notValidArray = Array.isArray(numbers); + if(!notValidArray) { + throw `Not a valid array`; + } let sum = 0; - - for(let i = 0; i < numbers.length; i ++) { - sum += numbers[i]; + for(let number of numbers) { + sum += number; } return sum; } -// Option 2 -// function sumNumbers(numbers) { -// let counter = 0; -// numbers.forEach(item => { -// counter += item; -// }) -// return counter; -// } - -// Option 3 +// Option 2 - Explicit return // function sumNumbers(numbers) { // // return the result of reduce // return numbers.reduce((a,b) => { // Explicit return, in more than 1 line, with {} and 'return' keyword @@ -64,27 +59,36 @@ function sumNumbers(numbers) { // },0); // } -// Option 3 +// Option 2.1- Implicit return // function sumNumbers(numbers) { // // return the result of reduce // return numbers.reduce((a,b) => a + b, 0);// Implicit return, in one line without the word 'return' // } // Iteration #3.1 Bonus: -//sum() that calculates the sum for array filled with (almost) any type of data. -//Note that strings should have their length added to the total, -//and boolean values should be coerced into their corresponding numeric values. - +// Let's implement the function sum() that calculates the sum for array filled with +//(almost) any type of data. Note that strings should have their length added to the +//total, and boolean values should be coerced into their corresponding numeric values. Check the tests for more details. +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; +// should return: 57 function sum(numbers) { + // check if it the argument is a valid array + let notValidArray = Array.isArray(numbers); + if(!notValidArray) { + throw `Not a valid array`; + } + let counter = 0; numbers.forEach(item => { + // check valid values let notValid = typeof item === 'object' || Array.isArray(item); + let isNumber = typeof item === 'number'; let isString = typeof item === 'string'; let isBoolean = typeof item === 'boolean'; - let isNumber = typeof item === 'number'; + // for objects or arrays, throw an error message if (notValid) { throw `Unsupported data type sir or ma'am`; } @@ -93,31 +97,29 @@ function sum(numbers) { counter += item; } else if (isString) { counter += item.length; - } else if (isBoolean) { - if (item === false) { - counter += 0; - } else if (item === true) { - counter += 1; - } + } else if (isBoolean) { // Number(true) --> 1 // Number(false) --> 0 + counter += Number(item); } }) return counter; } - - // Iteration #4: Calculate the average +// Find the sum as we did in the first exercise (or how about reusing that the sumNumbers()?) +// Take that sum and divide it by the number of elements in the list. + // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; - function averageNumbers(numbers) { //should return null if receives an empty array when called if(numbers.length === 0) { return null; } + let totalNumbers = numbers.length; let average; + // Reuse the sumNumbers function let sum = sumNumbers(numbers); average = sum / totalNumbers; @@ -127,65 +129,41 @@ function averageNumbers(numbers) { // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; +//Implement the function named averageWordLength that receives as a single argument +//an array of words and returns the average length of the words: function averageWordLength(words) { - let arrWordsLength = []; + let wordsLength = []; //iterate to create new array with length of words - words.forEach(item => { - arrWordsLength.push(item.length); + words.forEach(word => { + wordsLength.push(word.length); }); - // Average of numbers based on the Array of the strings - let avgNumbers = averageNumbers(arrWordsLength); + // Reuse the function 'averageNumbers' + // Average of numbers based on the length of words + let avgNumbers = averageNumbers(wordsLength); return avgNumbers; } -// function averageWordLength(words) { -// let sum = 0; -// let arrWordsLength = []; -// let totalItems = words.length; -// let totalAverage; - -// //iterate to create new array with length of words -// words.forEach(item => { -// arrWordsLength.push(item.length); -// }); -// //console.log(arrWordsLength); - -// //iterate to sum the length of numbers -// //Option 2 -- forEach loop -// arrWordsLength.forEach(item => { -// sum += item; -// }); - -// //Option 3 -- for loop -// // for(let i = 0; i < arrWordsLength.length; i ++) { -// // counter += arrWordsLength[i]; -// // //console.log(`counter: ${counter}`); -// // } -// totalAverage = sum/totalItems; -// //console.log(totalAverage) -// return totalAverage; -// } - -//const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; -// Expected --> 5.7 -// But there is an error on the tests--> expected 5.1 - // Bonus - Iteration #4.1 +// Create function avg(arr) that receives any mixed array and calculates average. +//Consider as mixed array an array filled with numbers and/or strings and/or booleans. We are following a similar logic to the one applied on the bonus iteration 4.1. function avg(arr) { - let totalItems = arr.length; // check if there is an empty array if(arr.length === 0) { return null; } - // Use Function Sum to iterate over the items and sum mixed items - let sumMixedItems = sum(arr); - let avgMixedItems = sumMixedItems/totalItems; + // Reuse function 'sum' to iterate over the items and sum mixed items + let sumMixedItems = sum(arr); + let avgMixedItems = sumMixedItems/arr.length; + // return result with 2 decimals return Number(avgMixedItems.toFixed(2)); } // Iteration #5: Unique arrays + +// Take the following array, remove the duplicates, and return a new array. +//You are more than likely going to want to check out the indexOf Array method. const wordsUnique = [ 'crab', 'poison', @@ -200,33 +178,48 @@ const wordsUnique = [ 'bring' ]; -//remove the duplicates, and return a new array. -//You are more than likely going to want to check out the indexOf Array method. - +// Option 1 - using indexOf +// indexOf --> method returns the first index function uniquifyArray(words) { if(words.length === 0) { return null; } let unique = []; - words.forEach( word => { - //console.log(`item: ${item}`); - // Checks if the word it's not included inside the array given 'words' - const isWordRepeated = unique.includes(word); - if(!isWordRepeated) {//is not included + words.forEach( (word, index) => { + // returns the first index of the array where is present the word given + let isPresentIndex = words.indexOf(word); // positive number --> it's present in the index + let notRepeated = isPresentIndex === index; // if the index are equal, is present, if it's different--> it's repeated + if(notRepeated) {//is not included unique.push(word); } }) return unique; } +// Option 2 -- using includes() +// function uniquifyArray(words) { +// if(words.length === 0) { +// return null; +// } +// let unique = []; + +// words.forEach( word => { +// //console.log(`item: ${item}`); +// // Checks if the word it's not included inside the array given 'words' +// const isWordRepeated = unique.includes(word); +// if(!isWordRepeated) {//is not included +// unique.push(word); +// } +// }) +// return unique; +// } // Iteration #6: Find elements -const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -//that will 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. -//that will 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. +// Declare a function named doesWordExist that will 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. +const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; + function doesWordExist(arrWords, word) { // return null if the array is empty if (arrWords.length === 0) { @@ -240,6 +233,8 @@ function doesWordExist(arrWords, word) { } } // Iteration #7: Count repetition +// Declare a function named howManyTimes that will take in an array of words as the first argument, +//and a word to search for as the second argument. The function will return the number of times that word appears in the array. const wordsCount = [ 'machine', 'matter', @@ -253,7 +248,7 @@ const wordsCount = [ 'disobedience', 'matter' ]; -// Iteration #7: Count repetition + function howManyTimes(arrWords, wordToCheck) { // if the array is empty, return 0 if(arrWords.length === 0) { @@ -273,9 +268,8 @@ function howManyTimes(arrWords, wordToCheck) { //howManyTimes(wordsCount, "matter") // 4 - - // Iteration #8: Bonus +// function named greatestProduct(matrix) to find it in the 20×20 grid below! const matrix = [ [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8], [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0], @@ -298,10 +292,11 @@ const matrix = [ [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54], [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] ]; - -function greatestProduct() {} - - +// Find the greatest product of four adjacent numbers +function greatestProduct(matrix) { + //elements adjacet to each other, horizontally or vertically + const n = 4; +} // The following is required to make unit tests work. From 376def95353f6ab489f8452d8b8ca994c032fd6c Mon Sep 17 00:00:00 2001 From: karlajaramillo Date: Mon, 20 Sep 2021 01:53:21 +0200 Subject: [PATCH 4/6] Add Iteration #8- Bonus- Product of adjacent numbers --- src/functions-and-arrays.js | 57 +++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index dd7a79ca3..df90ae701 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -292,12 +292,63 @@ const matrix = [ [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54], [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] ]; + +let matrixTest = [ +[ 1, 2, 3, 4, 5], +[ 1, 20, 3, 4, 5], +[ 1, 20, 3, 4, 5], +[ 1, 20, 3, 4, 5], +[ 1, 4, 3, 4, 5] +]; +// 32000 + // Find the greatest product of four adjacent numbers -function greatestProduct(matrix) { - //elements adjacet to each other, horizontally or vertically - const n = 4; +function greatestProduct(arr) { + //elements adjacent to each other, horizontally or vertically + // the number of elements adjacent should be greater than or equal to 4 + const adjacentItems = 4; + const adjacentToCurrent = adjacentItems -1; + // grid of 20x20 --> number of rows and columns + const grid = arr.length; + + let max = 0; + let result; + // iterate over rows - horizontally + for (let i = 0; i < grid; i++) { // loop over rows + // iterate over columns + for (let j = 0; j < grid; j++) { // loop over a column, inside each row + // check there are 4 adjacents inside the grid + // the position to start the calculation inside the row + // from left to right, position 0,1,2,'3, where '3' is the current j position + // Not count at: position j ->0, position->1, position->2. + // But start to count in position j->3 + // Check the maximum product --> horizontally in a row + if ((j - adjacentToCurrent) >= 0) { // j is the current, minus 3 items in the row + // calculate multiplication of the 4 items in the row, at positions: j, j-1, j-2, j-3 + result = arr[i][j] * arr[i][j - 1] + * arr[i][j - 2] * arr[i][j - 3]; + // if the result is greater than the max value + // assign/update max with the result + if (result > max) { + max = result; + } + } + + // Check the maximum product --> vertically in a colummn + if ((i - adjacentToCurrent) >= 0) { + result = arr[i][j] * arr[i - 1][j] + * arr[i - 2][j] * arr[i - 3][j]; + if (result > max) { + max = result; + } + } + } + } + return max; } +//Bonus - Iteration #8.1: Product of diagonals + // The following is required to make unit tests work. /* Environment setup. Do not modify the below code. */ From ac9a8938d7e1a63c5b45d6622b95fb37f48f30fd Mon Sep 17 00:00:00 2001 From: karlajaramillo Date: Tue, 21 Sep 2021 16:13:44 +0200 Subject: [PATCH 5/6] Add Iteration 8.1 - Bonus - Product of diagonals --- src/functions-and-arrays.js | 127 +++++++++++++++++++++++++++++++----- 1 file changed, 109 insertions(+), 18 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index df90ae701..505beab89 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -196,6 +196,11 @@ function uniquifyArray(words) { }) return unique; } +//uniquifyArray(["Cat", "Dog", "Cow", "Cat"]) +// Cat will be at indexOf === index --> 0 === 0 --> true, not repeated +// then, Cat will be at indexOf === index --> 3 === 0 --> +//indexOf find "Cat" at the first index, which is 0, they are not the same, so it's repeated + // Option 2 -- using includes() // function uniquifyArray(words) { // if(words.length === 0) { @@ -269,7 +274,7 @@ function howManyTimes(arrWords, wordToCheck) { // 4 // Iteration #8: Bonus -// function named greatestProduct(matrix) to find it in the 20×20 grid below! + //function named greatestProduct(matrix) to find it in the 20×20 grid below! const matrix = [ [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8], [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0], @@ -300,45 +305,66 @@ let matrixTest = [ [ 1, 20, 3, 4, 5], [ 1, 4, 3, 4, 5] ]; -// 32000 +// horizontally and vertically --> 32000 +// diagonal --> 1200 // Find the greatest product of four adjacent numbers +// the four numbers need to be adjacent to each other +// It can be top, down, left, rigth function greatestProduct(arr) { //elements adjacent to each other, horizontally or vertically // the number of elements adjacent should be greater than or equal to 4 - const adjacentItems = 4; - const adjacentToCurrent = adjacentItems -1; - // grid of 20x20 --> number of rows and columns - const grid = arr.length; - + const adjacentNumbers = 4; + // It must be 4 adjacent numbers, so, substract the current one to the others + const adjacentToCurrent = adjacentNumbers -1; + // for grid of 20x20 --> number of rows --> 20 + // for grid of 5x5 --> number of rows --> 5 + const grid = arr.length; // number of rows + // store the maximum product let max = 0; + // stored the multiplication of the 4 adjacent numbers let result; // iterate over rows - horizontally for (let i = 0; i < grid; i++) { // loop over rows - // iterate over columns + // iterate over columns from left -> to rigth for (let j = 0; j < grid; j++) { // loop over a column, inside each row // check there are 4 adjacents inside the grid // the position to start the calculation inside the row - // from left to right, position 0,1,2,'3, where '3' is the current j position + // from left --> to right, position 0,1,2,'3, where '3' is the current j position // Not count at: position j ->0, position->1, position->2. - // But start to count in position j->3 // Check the maximum product --> horizontally in a row + + // Horizontally + // From right to left - check max product + // Starting point that meets the condition--> i = 0, j = 3 + // from positions j >= 3, the condition is true + // from positions j < 3, the condition is false if ((j - adjacentToCurrent) >= 0) { // j is the current, minus 3 items in the row - // calculate multiplication of the 4 items in the row, at positions: j, j-1, j-2, j-3 + // calculate multiplication with the 4 items in the row, from right --> to left + // at positions: j, j-1, j-2, j-3 + // j is getting smaller, and i is the same row result = arr[i][j] * arr[i][j - 1] * arr[i][j - 2] * arr[i][j - 3]; - // if the result is greater than the max value + // if the result is greater than the max value // assign/update max with the result if (result > max) { - max = result; + max = result; // update } } - // Check the maximum product --> vertically in a colummn - if ((i - adjacentToCurrent) >= 0) { - result = arr[i][j] * arr[i - 1][j] + // Vertically + // From down to up - check the max product + // calculate multiplication with the 4 items in the row, from down to up + // starting point that meets the condition --> i=3, j=0 + // from positions i >= 3, the condition is true + // from positions i < 3, the condition is false + // i is getting smaller, and j is the same column) at positions: i, i-1, i-2, i-3 + if ((i - adjacentToCurrent) >= 0) { //i is current position, minus 3 items in the column + result = arr[i][j] * arr[i - 1][j] * arr[i - 2][j] * arr[i - 3][j]; - if (result > max) { + // if the result is greater than the max value + // assign/update max with the result + if (result > max) { max = result; } } @@ -348,8 +374,73 @@ function greatestProduct(arr) { } //Bonus - Iteration #8.1: Product of diagonals +function greatestProductOfDiagonals(arr) { + //elements adjacent to each other, horizontally or vertically + // the number of elements adjacent should be greater than or equal to 4 + const adjacentNumbers = 4; + // It must be 4 adjacent numbers, so, subtract the current one to the others + // The key is to know the number to subtract to the current position + const itemsToConsider = adjacentNumbers - 1; // 4 -1 --> 3 + + // for grid of 20x20 --> number of rows --> 20 + // for grid of 5x5 --> number of rows --> 5 + // arr.length; // number of rows to iterate + + // variables to evaluate the diagonal from down to up --> rigth direction + // last column: arr.length - 1 + const positionToEvaluateJ = (arr.length -1) - itemsToConsider; // 4 - 3 + + // store the maximum product + let max = 0; + // stored the multiplication of the 4 adjacent numbers + let result; + // iterate over rows - horizontally + //i < grid --> this condition mark the area of the grid or delimit the grid + for (let i = 0; i < arr.length; i++) { // loop over rows + // iterate over columns from left -> to rigth + for (let j = 0; j < arr.length; j++) { // loop over a column, inside each row + // delimit the starting point --> + // Represents the diagonal --> Going through down to right + // Diagonal from: i = 0, j=0, starting point that meets the condition --> i = 3, j = 3 + if ((i - itemsToConsider) >= 0 && (j - itemsToConsider) >= 0) { // j is the current, minus 3 items in the row + // Evaluates to true--> at i = 3, j = 3 + // condition--> (i - itemsToRemove) >= 0 --> i - 3 >= 0 + // condition--> (j - itemsToRemove) >= 0)--> i - 3 >= 0 + // calculate with items in diagonal up to left --> j and i decreases 1 position + // From--> i = 3, j = 3--> arr[3][3], start the calculation "up to the left" + result = arr[i][j] * arr[i - 1][j - 1] + * arr[i - 2][j - 2] * arr[i - 3][j - 3]; + // if the result is greater than the max value + // assign/update max with the result + if (result > max) { + max = result; + } + } + // Represents the diagonal --> Starting at down to up to right + // + // Diagonal from: i = 0, j = 4, move 3 positions down--> i = 3, and 3 positions left j = 1 + // lastColumn = arr.length - 1 + // items to consider apart from the current items is --> 3 + // j <= ((arr.length -1) - 3 ) // (5 - 1) - 3 --> 4 - 3 --> 1 + // j<= last position of the column minus 3 items to consider + // j <= 1 + if ((i - itemsToConsider) >= 0 && j <= positionToEvaluateJ ) { + // the columns j=0, and j=1 are the finish points + // From--> i = 3, j = 1--> arr[3][1], start the calculation "up to the right" + result = arr[i][j] * arr[i - 1][j+1] + * arr[i - 2][j+2] * arr[i - 3][j+3]; + // if the result is greater than the max value + // assign/update max with the result + if (result > max) { + max = result; + } + } + } + } + return max; +} // The following is required to make unit tests work. /* Environment setup. Do not modify the below code. */ if (typeof module !== 'undefined') { @@ -366,4 +457,4 @@ if (typeof module !== 'undefined') { howManyTimes, greatestProduct }; -} +} \ No newline at end of file From 24a2bba139e8fd435c8d206718fe9b671dc0f8cd Mon Sep 17 00:00:00 2001 From: karlajaramillo Date: Tue, 21 Sep 2021 17:04:22 +0200 Subject: [PATCH 6/6] Add Iteration 5 - Unique Arrays - with indexOf method --- src/functions-and-arrays.js | 41 ++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 505beab89..c82b875e5 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -25,7 +25,7 @@ function findLongestWord(words) { words.forEach( word => { //check if the length of the word is > the maximum length if(word.length > longestWord.length) { - longestWord = word; // assign/update with the longest word + longestWord = word; // update with the longest word } }) return longestWord; @@ -81,7 +81,7 @@ function sum(numbers) { let counter = 0; - numbers.forEach(item => { + numbers.forEach(item => { // check valid values let notValid = typeof item === 'object' || Array.isArray(item); let isNumber = typeof item === 'number'; @@ -132,16 +132,18 @@ const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smar //Implement the function named averageWordLength that receives as a single argument //an array of words and returns the average length of the words: function averageWordLength(words) { - let wordsLength = []; - + if(words.length === 0) { + return null; + } + + let wordsTotal = words.length; + let sumLengthWords = 0; + //iterate to create new array with length of words - words.forEach(word => { - wordsLength.push(word.length); - }); - // Reuse the function 'averageNumbers' - // Average of numbers based on the length of words - let avgNumbers = averageNumbers(wordsLength); - return avgNumbers; + for (let word of words) { + sumLengthWords += word.length; + } + return sumLengthWords / wordsTotal; } // Bonus - Iteration #4.1 @@ -152,10 +154,9 @@ function avg(arr) { if(arr.length === 0) { return null; } - // Reuse function 'sum' to iterate over the items and sum mixed items let sumMixedItems = sum(arr); - let avgMixedItems = sumMixedItems/arr.length; + let avgMixedItems = sumMixedItems / arr.length; // return result with 2 decimals return Number(avgMixedItems.toFixed(2)); } @@ -186,21 +187,15 @@ function uniquifyArray(words) { } let unique = []; - words.forEach( (word, index) => { + words.forEach( word => { debugger // returns the first index of the array where is present the word given - let isPresentIndex = words.indexOf(word); // positive number --> it's present in the index - let notRepeated = isPresentIndex === index; // if the index are equal, is present, if it's different--> it's repeated - if(notRepeated) {//is not included + let notRepeated = unique.indexOf(word) === -1 ; // -1 is not present + if(notRepeated) {// push the word that is not repeated unique.push(word); } }) return unique; } -//uniquifyArray(["Cat", "Dog", "Cow", "Cat"]) -// Cat will be at indexOf === index --> 0 === 0 --> true, not repeated -// then, Cat will be at indexOf === index --> 3 === 0 --> -//indexOf find "Cat" at the first index, which is 0, they are not the same, so it's repeated - // Option 2 -- using includes() // function uniquifyArray(words) { // if(words.length === 0) { @@ -263,7 +258,7 @@ function howManyTimes(arrWords, wordToCheck) { //iterates to check if the wordToCheck is included inside the array of words arrWords.forEach(word => { //let isWordIncluded = word.includes(word); - let isWordIncluded = word === wordToCheck; + let isWordIncluded = word === wordToCheck; //is the same word if(isWordIncluded) { counter +=1; }