Skip to content
Advertisement

API returns undefined after 2nd round loop

I am getting PI number from PI Api (https://pi.delivery) and checking for palindromic of 21 numbers in sequence on each 105 numbers block (5 x 21) to be faster. My code returns undefined on the second round of for loop. Why is this happening?

PS: This is a 105 numbers block and 2 rounds loop just for testing purpose.

JavaScript

Advertisement

Answer

Pay attention to your group and for (let j = group...). You have for (let i = 0; i < 5; i += 1) and in the end of this loop you are adding 21 to group. For first y loop everything works, but for 2nd y loop you are not resetting group back to 0, so it keeps growing and due to that your j variable becomes bigger than the length of the pi variable. So you are starting getting “undefined” then. Probably you need to move let group = 0; a bit down, into the beginning of the for (let y = 0; y < 2; y += 1) loop.

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