Skip to content
Advertisement

How to convert ‘start’, ‘stop’ and ‘step’ values in a for loop

Given the start, stop and step values, where start > stop and step < 0, how can I convert the values so that start < stop and step > 0 and use these values in a for loop that can yield the same index but in a reverse way. In other words, what should be the mathematical formula to use to set variables converted_start and converted_stop below?

JavaScript

Advertisement

Answer

Here come the formula:

JavaScript

Here come the tests:

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