mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
Project Euler problems (numbering) : follow naming convention.
This commit is contained in:
26
Project-Euler/Problem008.js
Normal file
26
Project-Euler/Problem008.js
Normal 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 (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 }
|
Reference in New Issue
Block a user