Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FT_RMT_112024 MINE #334

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 87 additions & 12 deletions src/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,44 @@ 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;
}




// 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;
}



Expand All @@ -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));



Expand All @@ -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],
Expand All @@ -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));