Why is this recursive function only returning the starting number?

This range is only returning the starting number. why is it that the condition (startNum <= endNum) differs from (startNum - endNum === 0) ?

function rangeOfNumbers(startNum, endNum) {
  if (startNum <= endNum) {
    return [startNum];
  } else {
    let numbers = rangeOfNumbers(startNum + 1, endNum);
    return numbers;

console.log(rangeOfNumbers(6, 9));

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

The first number passed into the function is always going to be less than or equal to the second number passed into the function, so startNum <= endNum is always going to be true and thus your function is always going to return [startNum], hence no recursion.

startNum <= endNum is true if startNum is lower or equal to endNum
startNum - endNum === 0 is true if the subtraction is 0, so if startNum and endNum are equal.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.