• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

412
Views
Nested functions is not working but using parameter works

The sumAll function should return the sum of each number between two numbers. If the two numbers is 1 and 4 the output should be 10 since its 1 + 2 + 3 + 4 = 10. I made two nested functions which are least and greater:

  const least = (num1, num2) => {
    if (num1 > num2) {
      return num1;
    } else if (num2 > num1) {
      return num2;
    }
  }

  const greater = (num1, num2) => {
    if (num1 > num2) {
      return num1;
    } else if (num2 > num1) {
      return num2;
    }
  }

least should find which number is the least and greater should find which one is greater.

I called the nested function like this:

  for (let i = least(num1, num2); i <= greater(num1, num2); i++) {
    sum += i;

  }
  return sum;

};

But it didn't work since the output is 4. The i should have the least number in it and in the i <= greater(num1, num2) the greater should have the greater number. It doesn't work when I use the nested functions but it works when I use num1 and num2 like this:

for (let i = num1; i <= num2; i++)

What should I do to make this work using the nested functions?

Here's the fiddle: https://jsfiddle.net/d45v86kf/2/

almost 3 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error