Can anyone help me a bit more with regards to the base return value, I’m not exactly sure when I can use an array or a “normal” value, this has been “bugging” me ever since I started using recursion and up til now I’m still not sure exactly what goes into this value.
One thing I have noticed…is that when Im creating an array using the push/unshift (etc) that the base return has to be in array format otherwise the function won’t work. Am I answering my own question here or what. Perhaps if the array has already been created (where push/unshift isn’t used) that return value can be anything really (anything except array format). Am I suppose to use the return with an array when push/unshift is used?

`````` **Your code so far**
``````
``````
function rangeOfNumbers(startNum, endNum) {
if (startNum>endNum){
return [];
}else{
var arr=rangeOfNumbers(startNum+1,endNum)
arr.push(startNum);
return arr;
}};

rangeOfNumbers(4,10)
console.log(rangeOfNumbers(4,10))

``````
`````` **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36`

Challenge: Use Recursion to Create a Range of Numbers

By definition, the function returns an array of integers, so you always have to return an array of integers, which means both of the return statements in your function must return an array of integers. This is why the base case has to return an array of integers or it won’t work.

Looking at your base case, is it returning an array of at least one integer? If not then it is not correct. The last line in the instructions is giving you a big hint about the base case:

“It should also work for cases where both `startNum` and `endNum` are the same.”

What should you return if both `startNum` and `endNum` are the same? This is your base case.

1 Like

This code (with return empty square brackets) does work…in fact the whole block works in any case and it returns exactly how it should…I’m just wondering, if i were to put other values in (except arrays on the return on base case) if it’s important to put an array in or can I put another value in (in this case putting another value gives an error) hence my question, is there a relationship between building an array (with a return as an array in base case) and the push method (in building an array)

Sorry, I guess I’m missing something here then because when I run the code you pasted above it does not pass all the tests.

As I said, your function must always return an array with at least one number in it. That’s the reason the base case has to return an array. Look at your `else` statement:

``````var arr=rangeOfNumbers(startNum+1,endNum)
arr.push(startNum);
``````

The variable `arr` must be an array or how are you going to call the `push` method on it? So `rangeOfNumbers` must return an array.

Oh I’m sorry, I wasn’t intending this piece of code to pass any tests, but if you run in an editor it gives an array, I was just using as example to point out my question, that’s all lol, the question with regards to what needs to be returned within the base case . I sometimes get very confused around that actually. Can there sometimes be a number return or is it always an array that’s returned (be it empty or have an element number within)
thanks

The requirement for the function is:

“The function should return an array of integers”

So the function must always return an array with at least one integer. This applies to any return statement in the function body.

If you mean recursion in general, a recursive function needs to return what you need it to return. It’s just a function: if it needs to return an array then return an array. If it needs to return a number then return a number

The function `rangeOfNumbers` is supposed to take a start number and an end number and generate an array from start to end. So if the function is to return an array, you need to code it to return an array rather than any other type of thing

Yeah, you see, this is my problem…I don’t know when to return an array or when to return just a value? I’m not sure…

You’re writing a function that returns an array of numbers, which means you need to return an array of numbers.

Well then any recursion will require an array as return really?

You can use recursion to calculate the factorial of a number, in which case the recursive function should return a number. You can also use recursion to build an array of numbers, in which case the function should return an array.
It really depend on what you want to achieve. Recursion itself doesn’t require a specific type of output, recursion is the concept of a function calling itself until a certain condition is met.

Remember, even though you are using recursion to solve a problem you are still just creating a function that returns a value. It’s the requirements for what the function returns that determines what you need to return. If your function needs to return a number then you must always return a number. If it needs to return an array then you must always return an array.

Pretend you weren’t using recursion for the specific function we are talking about. Knowing that the requirements for the function say that it must return an array would you ever return anything else than an array? Just because you are using recursion does not change this requirement.

If you have a function, and the point of a function is to return an array, then you have to return an array. You don’t have a choice. If a function is meant to return the string “hello”, it has to return the string “hello”, it can’t return the number 3.

Whether a function uses recursion or not, you still have to return the thing you want to return, JavaScript can’t read your mind and figure out that for that function you actually wanted to return something else. In the case of this problem, if you return a number from the function, the function will return a number – it isn’t magic, the code can’t “know” that in fact you wanted to return an array.

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