Added ProjectEuler problem 8 with passed test cases

This commit is contained in:
Omkarnath Parida
2021-10-02 23:45:28 +05:30
parent 15835ede13
commit 8b1d10df78
2 changed files with 130 additions and 0 deletions

26
Project-Euler/Problem8.js Normal file
View File

@ -0,0 +1,26 @@
// Problem: https://projecteuler.net/problem=8
const largestAdjacentNumber = (grid, consecutive) => {
grid = grid.split('\n').join('');
const splitedGrid = grid.split("\n");
let largestProd = 0;
for (let row in splitedGrid) {
const currentRow = splitedGrid[row].split('').map(x => Number(x));
for (let i = 0; i < currentRow.length - consecutive; i++) {
const combine = currentRow.slice(i, i + consecutive);
if (!combine.includes(0)) {
const product = combine.reduce(function (a, b) {
return a * b
});
if (largestProd < product) largestProd = product;
}
}
}
return largestProd;
}
export { largestAdjacentNumber };