From 37d36db0215cb497278aeb3e035d542719399c53 Mon Sep 17 00:00:00 2001 From: Mine Cetinkaya Date: Thu, 21 Nov 2024 17:59:24 +0100 Subject: [PATCH] solved --- src/challenges.js | 99 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 87 insertions(+), 12 deletions(-) diff --git a/src/challenges.js b/src/challenges.js index 940a599..76d8865 100644 --- a/src/challenges.js +++ b/src/challenges.js @@ -13,13 +13,32 @@ const repeatedWords = [ "matter" ]; -function howManyTimes() {} +function howManyTimes(repeatedWords, word) { + let countRepeat = 0; + for( const elem of repeatedWords){ + if(elem === word){ + countRepeat++; + } + } + return countRepeat; +} // Iteration 2 | Number Sequence -function createSequence() {} +function createSequence(number) { + let numVal = 0; + let result = []; + if (number === 0){ + return result; + } + while(numVal <= number){ + result.push(numVal); + numVal++; + } + return result; +} @@ -27,7 +46,11 @@ function createSequence() {} // Iteration 3 | Multiply for Each const numbers = [1, 2, 5, 10, 13, 50]; -function multiplyBy() {} +function multiplyBy(numbers, multiplier) { + let result = []; + numbers.forEach(elem => result.push(elem * multiplier)); + return result; +} @@ -36,8 +59,22 @@ function multiplyBy() {} const original = ["cat", "dog", "fish", "bird", "cat", "fish"]; const toRemove = ["cat", "dog"]; -function filterOut() {} - +function filterOut(original, toRemove) { + let indRemove = 0; + if(original.length === 0){ + return null; + } + for(const elem of toRemove){ + for(const item of original){ + if(item === elem){ + indRemove = original.indexOf(item); + original.splice(indRemove, 1); + } + } + } + return original; +} +console.log(filterOut(original, toRemove)); @@ -55,14 +92,25 @@ const duplicateWords = [ "simple", "bring" ]; - -function uniquifyArray() {} - +//indexof includes +function uniquifyArray(duplicateWords) { + let result = []; + if(duplicateWords.length === 0){ + return null; + } + duplicateWords.forEach(function (element){ + if(!result.includes(element)){ + result.push(element); + } + }) + return result; +} +console.log(uniquifyArray(duplicateWords)); // Bonus: Iteration 6 | Product of Adjacent Numbers -const matrix = [ +/*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], [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65], @@ -83,6 +131,33 @@ const matrix = [ [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], [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] -]; - -function greatestProduct() {} +];*/ +const matrix =[[ 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]]; +function greatestProduct(matrix) { + // assuming nxn matix and positive numbers + let maxProduct = 1; + let tmpProduct = 1; + for(let i = 0; i < matrix.length; i++){ + for( let j = 0; j <= matrix.length - 4; j++){ + tmpProduct = matrix[i][j] * matrix[i][j+1] * matrix[i][j+2] * matrix[i][j+3]; + if (tmpProduct > maxProduct){ + maxProduct = tmpProduct; + } + } + } + for( let j = 0; j < matrix.length; j++){ + for(let i = 0; i <= matrix.length -4 ; i++){ + + tmpProduct = matrix[i][j] * matrix[i+1][j] * matrix[i+2][j] * matrix[i+3][j]; + if (tmpProduct > maxProduct){ + maxProduct = tmpProduct; + } + } + } + return maxProduct; +} +console.log(greatestProduct(matrix));