B
B
Bohdan Petrov2019-11-16 21:39:42
JavaScript
Bohdan Petrov, 2019-11-16 21:39:42

Autocorrect increment/decrement i++ => i+=1?

I encountered a bug in the code, the essence of which was to use i++ instead of ++i .
I decided to find a solution for autocorrecting i++ to i+=1, but I didn’t find anything really on the net.
I use Webstorm and a bunch of ESLint + Prettier. There is a no-plusplus rule, it highlights everything.
Interested in the possibility of auto-formatting, how var automatically changes to let, etc.
Here is an example of code in which I caught an error:

function smallestDivisor(num) {
  function process(num, divisor) {
    if (num === 1) {
      return 1;
    }
    if (num === 0) {
      return 0;
    }
    if (num % divisor === 0) {
      return divisor;
    }
    if (divisor === num) return num;
    return process(num, ++divisor );
  }
  return process(num, 2);
}
console.log(smallestDivisor(25));

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question