Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
'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!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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)}`);

about 4 years ago · Juan Pablo Isaza Report

0

If you call return within the loop, the loop will only be executed once. You need to move the return statement outside of the loop. Whenever calling return, this will cause the current function to exit.

function IncrementNumber(num, iteratorMax){
  for (let i = 0; i < iteratorMax; i++) {
    num += i;
  }
  
  return num;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!