Skip to content
Advertisement

Stop fibonacci sequence when it reaches a number bigger than 1000

I want to make the sequence stop when the last value stored in the array is bigger than 1000.

I have the js code for the sequence and i can make it stop at whatever position i want with the variable limit. But i dont know how to make it stop at a certain number.

The is my code:

JavaScript

Advertisement

Answer

You can just use an if condition and break out of the loop as soon as a value is bigger than 1000. In this example I pushed the value and then break out of the loop. You can also break before pushing it to the array

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