Skip to content
Advertisement

Getting strange errors when breaking up a long number into its component integers

This is for Project Euler’s 8th question, which asks you to find the greatest product of five consecutive digits in this one really long number.

My code is definitely NOT the most elegant solution, but I’m pretty sure it should work. And it does seem to work with small numbers, but once the number I test is greater than 16 digits, everything starts to fall apart (e.g. The subsets are getting e’s and 0’s and different numbers instead of the digits in the actual given number.)

JavaScript

I have been testing it with Codecademy’s scratchpad, maybe that is part of the problem. I just started learning js last week and only began these Euler problems yesterday, so I could be completely screwing this in a whole universe of ways. Any ideas?

Advertisement

Answer

As long as you pass the digits as a string you can use your method,

but most browsers have a forEach method that can simplify it for you.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement