Ad
  • Default User Avatar

    For this specific instance, its completely arbitrary. Doesn't make one difference.

    There is a difference if you are wondering though. In the solution, he uses a pre-increment rather than a post increment like in your question. It increments the number before the statement executes. It doesn't make a difference because the only thing in that statement is '++i'. Here's an example of it:

    let num1 = 0;
    console.log(num1++) // Outputs 0. The line executes then num1 increments after. This is called a 'post-increment'
    console.log(num1) // Outputs 1.
    
    let num2 = 0;
    console.log(++num2) // Outputs 1. It increments BEFORE the line executes. This is called a 'pre-increment'
    console.log(num2) // Outputs 1. 
    
    

    FUN FACT: There is another difference but I'm not sure it applies to Javascript, but in C++ my professor once told me that, in a for-loop, pre-increments are much more efficient, and that big data companies always use pre-increments when applicable because of that. Could be the reason why its used here, I know I use it because of that fact lol.

  • Default User Avatar

    Can someone explain why this wouldn't be used compared to other strategies?

  • Default User Avatar

    It's probably just because of the context of the question.

    Like if someone asked "Hey how do I do this thing in Python?" you wouldn't answer "Well in C++ you can do it like this..."

    So in this case, a function with a passed in parameter is given to you, might as well give the solution in the way it was already structured.
    Though I do agree that overall, the best practice for legibility in Javascript would be having the solution as a variable function.