'For loop' within a function

I hope everyone is having an amazing day so far.

I am asking for help on an exercise that I am stuck on. I have gone through and researched all over but still am not getting it right.

The task is:

Create a for loop in which an iterator starting from 0 up to the iteratorMax value (not included) (the second parameter to your function) is incremented by 1. Add the iterator to num(the first parameter to your function) for each incrementation. you must return the number at the end of your function.

I was given this code to start with:

    function IncrementNumber(num, iteratorMax){
        //Add For loop here
    //return value here
    }

I have tried coding it a number of different ways but still not correct.

for example:

    function IncrementNumber(num, iteratorMax){
    for (let i = 0; i < iteratorMax; i++) {
            return(i);
        }

    }

I have tried to declare "i" but that seems to be incorrect also.

I really appreciate any help or hints to what I am missing or doing wrong in my code.

Thank you!


Solution 1:

For the IncrementNumber(value, N) method, when value is 0, this algorithm returns the result of the equation:

Total = N x (N-1) / 2

function IncrementNumber(num, iteratorMax){
  for(let i = 0 ; i < iteratorMax ; ++i){
      num += i;
      console.log(`i: ${i}\tnumber: ${num}`);
  }
  return num;
}

console.log(`Result: ${IncrementNumber(0, 10)}`);