so we first start with a function
a blank function basicly looks like this
function myFunctionName() {
}
We then added two value's that can hold a number and gave them a fitting name.
Then we implemented an if else statement in the code
if () {
}else {
}
If sets the 1st condition else is about what happens if the if returns Boolean false.
For the first condition we sad aNum minus startNum has to be equal zero
If that is the case it will return and array see the [] which are commonly used to declare an array/object type this is one of the 9 concepts JS reconizes.
Now if this if statement is true it will call up the second vale of itself. It recals it own startNum. After that we close it with a semicolon.
Then comes the else statement which we already exaplained before
else will return a variable called numbers which holds the function we started with. only this time we say that endNum will be minus one
then comes the numbers which we just declared and tell it to push endNum to its back. Push pushes adds a new item to the end of the array we declared befor.
We then return the numbers.
So internally this is what is happening:
call rangeOfNumbers(5, 10)
call rangeOfNumbers(5, 9)
call rangeOfNumbers(5, 8)
call rangeOfNumbers(5, 7)
call rangeOfNumbers(5, 6) - this gets evaluated first then previous one and so on(last in first out principle)
Soon as the call stack is prepared to be evaluated, each function call is going to push the value to the numbers array. (rangeOfNumbers has a closure over the numbers value)
Now let’s look at the call stack again when is almost exhausted, have a look at the last execution and the state of the numbers array.