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/