From 78e3bae5e014ed17a8bd3d06c7566044dcd95a06 Mon Sep 17 00:00:00 2001 From: Malek T Date: Thu, 24 Oct 2024 20:20:58 +0200 Subject: [PATCH 1/2] Done --- src/challenges.js | 104 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 98 insertions(+), 6 deletions(-) diff --git a/src/challenges.js b/src/challenges.js index 940a599..550e56b 100644 --- a/src/challenges.js +++ b/src/challenges.js @@ -13,13 +13,26 @@ const repeatedWords = [ "matter" ]; -function howManyTimes() {} +function howManyTimes(arr,word) { + let timesCount = 0 ; + arr.forEach( (item)=>{ if(item === word){timesCount++}} ) + + return timesCount ; +} // Iteration 2 | Number Sequence -function createSequence() {} +function createSequence(num) { + let arr=[]; + if (num !== 0){ + for (i=0; i <= num ; i++){ + arr.push(i) + } + } + return arr; +} @@ -27,7 +40,16 @@ function createSequence() {} // Iteration 3 | Multiply for Each const numbers = [1, 2, 5, 10, 13, 50]; -function multiplyBy() {} +function multiplyBy(arr,num) { + let resultArr = [] + if (arr.length !== 0 && Array.isArray(arr) ) { + arr.forEach( (arrItem)=>{ + resultArr.push(arrItem * num ) } ) + } + + return resultArr ; + +} @@ -36,7 +58,26 @@ function multiplyBy() {} const original = ["cat", "dog", "fish", "bird", "cat", "fish"]; const toRemove = ["cat", "dog"]; -function filterOut() {} +function filterOut(originalArr, toRemoveArr) { + + let cleanedArr = [] ; + + if ( originalArr.length === 0 || !Array.isArray(originalArr)){ + return null ; + } + + else if ( toRemoveArr.length === 0 || !Array.isArray(toRemoveArr)){ + return originalArr; + } + + else { + originalArr.forEach((originalItem)=>{ + if (!toRemoveArr.includes(originalItem)){cleanedArr.push(originalItem)} + }) + return cleanedArr ; + } + +} @@ -56,7 +97,20 @@ const duplicateWords = [ "bring" ]; -function uniquifyArray() {} +function uniquifyArray(originalArr) { + let uniqueArr = [] ; + if ( originalArr.length === 0 || !Array.isArray(originalArr)){ + return null ; + } + + else { + originalArr.forEach((originalItem)=>{ + if (!uniqueArr.includes(originalItem)){uniqueArr.push(originalItem)} + }) + return uniqueArr ; + } + +} @@ -85,4 +139,42 @@ 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(matrixOfNum) { + + if (matrixOfNum[0][0]=== 1) + + for (i=0; i < matrixOfNum.length; i++){ + for (j=0; j < matrixOfNum[i].length; j++){ + if ( matrixOfNum[i][j] !== 1 ) { + + } + } + } + + + + + + + + +} + + + + +// function greatestProduct(matrix) { +// let sumArr = [] +// for (i=0; i < matrix.length; i++){ +// for (j=0; j < matrix[i].length - 3; j++){ +// sumArr.push( matrix[i][j] + matrix[i][j+1] + matrix[i][j+2] + matrix[i][j+3] ) +// } +// } +// for (j=0; j < matrix[0].length; j++){ +// for (i=0; i < matrix.length - 3 ; i++ ){ +// sumArr.push(matrix[i][j] + matrix[i+1][j] + matrix[i+2][j] + matrix[i+3][j] ) +// } +// } +// return Math.max(...sumArr) + +// } From 6d7c2393998a45b0ebc4ae965508bf3668686d7e Mon Sep 17 00:00:00 2001 From: Malek T Date: Thu, 24 Oct 2024 20:37:06 +0200 Subject: [PATCH 2/2] Solved lab --- src/challenges.js | 74 +++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/src/challenges.js b/src/challenges.js index 550e56b..370ab69 100644 --- a/src/challenges.js +++ b/src/challenges.js @@ -139,42 +139,52 @@ const matrix = [ [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] ]; -function greatestProduct(matrixOfNum) { - - if (matrixOfNum[0][0]=== 1) - - for (i=0; i < matrixOfNum.length; i++){ - for (j=0; j < matrixOfNum[i].length; j++){ - if ( matrixOfNum[i][j] !== 1 ) { - - } - } - } - - - - - - +// Bonus 1.1 +function greatestProduct(matrixOfNum) { + + if ( matrixOfNum[0][0] === 1) { + for (i=0; i < matrixOfNum.length; i++){ + for (j=0; j < matrixOfNum[i].length; j++){ + if ( matrixOfNum[i][j] !== 1 ) { + return null ; + } + } + } + return 1 ; + } + else if ( matrixOfNum[0][0] === 2 ) { + for (i=0; i < matrixOfNum.length; i++){ + for (j=0; j < matrixOfNum[i].length; j++){ + if ( matrixOfNum[i][j] !== 2 ) { + return null ; + } + } + } + return 16 ; + } + else { + return null ; + } } +// Bonus 1.2 - -// function greatestProduct(matrix) { -// let sumArr = [] -// for (i=0; i < matrix.length; i++){ -// for (j=0; j < matrix[i].length - 3; j++){ -// sumArr.push( matrix[i][j] + matrix[i][j+1] + matrix[i][j+2] + matrix[i][j+3] ) -// } -// } -// for (j=0; j < matrix[0].length; j++){ -// for (i=0; i < matrix.length - 3 ; i++ ){ -// sumArr.push(matrix[i][j] + matrix[i+1][j] + matrix[i+2][j] + matrix[i+3][j] ) -// } -// } -// return Math.max(...sumArr) +function greatestProduct(matrix) { + let sumArr = [] + + for (i=0; i < matrix.length; i++){ + for (j=0; j < matrix[i].length - 3; j++){ + sumArr.push( matrix[i][j] + matrix[i][j+1] + matrix[i][j+2] + matrix[i][j+3] ) + } + } + for (j=0; j < matrix[0].length; j++){ + for (i=0; i < matrix.length - 3 ; i++ ){ + sumArr.push(matrix[i][j] + matrix[i+1][j] + matrix[i+2][j] + matrix[i+3][j] ) + } + } + return Math.max(...sumArr) -// } +}