mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
27 lines
688 B
JavaScript
27 lines
688 B
JavaScript
// Problem: https://projecteuler.net/problem=8
|
|
|
|
const largestAdjacentNumber = (grid, consecutive) => {
|
|
grid = grid.split('\n').join('')
|
|
const splitedGrid = grid.split('\n')
|
|
let largestProd = 0
|
|
|
|
for (const 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 }
|