Is there any error in this code i am doing one of the exercise of basic javascript course (last One) output is coming but its showing wrong

let arr=[];

function rangeOfNumbers(startNum, endNum) {

  if(startNum===endNum){

      return startNum;

  }

  else{

      arr.push(startNum)

      rangeOfNumbers(startNum+1,endNum);

      return arr;

  }

};

let result=rangeOfNumbers(4,4);

console.log(result);

Do you have a question? What do you need help with? Please explain?

Also, please provide the link to the challenge that you are working on.


It looks like you are using faux-recursion with a global variable. You don’t want to use global variables this way. It results in buggy functions that can’t be called more than once. You need to use the actual return value of the function calls.

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like