Basic JavaScript - Use Recursion to Create a Range of Numbers

**Tell us what’s happening:**Hi, I see most of the posts are links and of the rest the code is hidden so feel okayish to ask here. My code seems to be an array to me but all my efforts have been blocked despite working fine and looking like arrays with 1,2,3,4 format etc. This one I altered to have the return that I thought had to be kept in the end but still don’t see that as the issue. I was very nearly confusedly pleased with having got the function to return itself. Also then it returns after the first close to go back which seemed the idea too, thanks. It would be great to complete 100 on this.
Describe your issue in detail here.

Your code so far

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

Your browser information:

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

Challenge: Basic JavaScript - Use Recursion to Create a Range of Numbers

Link to the challenge:

rangeOfNumbers ultimately needs to return a single array of numbers. So that means that rangeOfNumbers must always return a single array of numbers, know matter where you call it. So does returning j fulfill that requirement?

I suppose since it runs all the numbers barring the first one I could drop the first declaration and go startNum-1 inside the rangeOfNumbers function?

This is solution. If I had written it I would have wondered if the comma meant it was another array like mine. Just because it has a name j doesn’t make it a second one . Surely this is the same mistake. I think this is solution 3. I had to go and figure since had a problem that it worked and was an array even if 2 in a way, thus the answers I was getting suggested I wasn’t advanced enough to easily see other solutions.
return endNum < startNum
?
: [rangeOfNumbers(startNum, endNum - 1), endNum];

Did you solve this yourself or did you give up and look at the answer? You didn’t properly format the code you pasted so I can’t tell if the solution you pasted in is actually correct. As I see it, it is not.

Also, you don’t need to post solutions in here, especially ones from the hints, as we can see them for ourselves is we want. But if you do feel the need to post your own solution then please wrap it in spoiler tags:

[spoiler]
your code here (following the instructions below)
[/spoiler]

To display your code in here you need to wrap it in triple back ticks. On a line by itself type three back ticks. Then on the first line below the three back ticks paste in your code. Then below your code on a new line type three more back ticks. The back tick on my keyboard is in the upper left just above the Tab key and below the Esc key. You may also be able to use Ctrl+e to automatically give you the triple back ticks while you are typing in the this editor and the cursor is on a line by itself. Alternatively, with the cursor on a line by itself, you can use the </> button above the editor to add the triple back ticks.

I have not moved to finishing with other solutions yet. It seems that one is very similar to mine except for the ternary style. Sorry to have put in that solution claim ‘other persons code’ without the correct style.

ps I noticed three point full stops in it too which may be a clever way to do something about not being acceptable but don’t know how.

Yes, those three dots before the function are definitely important. That solution won’t work without them. If you don’t understand what they are doing at this point then I would just forget about this solution for now and concentrate on solving this problem the way you know how.

I was tending to agree but based on what was in the brackets. I really don’t see that giving it a name j is the thing that makes it an array in an array.

[startNum -1, rangeOfNumbers(startNum, endNum)] ;

This makes it an array in an array in an array…

Remember, rangeOfNumbers should always return a single array of numbers. So the line of code above sticking an array of numbers inside an array.

[startNum , rangeOfNumbers(startNum +1, endNum)] ;

would this be better then. If only the function applies the updates?

You still have the same problem:

[startNum , rangeOfNumbers(startNum +1, endNum)]

rangeOfNumbers should always return a simple array with numbers in it. So this line is putting an array of numbers inside an array.

Forget about the solution in hint 3, I think it is throwing you off. The only time you need to manually create an array using the square brackets is in the base case, where you return an empty array.

If you called this function as rangeOfNumbers(1, 1) then you know that it should return [1]. But you also know that you need to keep doing the recursion until you get down to the base case. So how should you call the function recursively so that endNum < startNum when you make the recursive call? And then when you do reach the base case the recursive call will return []. So what do you need to do with that in order to return [1]?

1 Like

I am getting the right answer but not the array inside thing. So calling (1,1) to get 1 is not the problem.

I used this example to make it very simple to see how recursion works because you will hit the base case immediately.

rangeOfNumbers(1,1)

When that function call executes you know it won’t hit the base case because endNum is not less than startNum. So you will need to make a recursive call. You also know that this recursive call should hit the base case. So what would the recursive call be in order to hit the base case? And you know that this recursive call will return []. So after you get the return value of the recursive call, what do you need to do to it so that your original function call returns [1]?

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