Returning values of a function

Is there a way to return the values of a function when the tests are run on a challenge by using console.log ? I am using the following code but not seeing any of the results from the function just the results from the tests.

here is the code i am using I have also put the console.log statement just below the push line and didn’t get any output

function rangeOfNumbers(startNum, endNum) {
  if (startNum == endNum) {
return [];
} else {
const countArray = rangeOfNumbers(endNum - 1);
countArray.push(endNum);
return countArray;
console.log(countArray);
}
  
}

Anything after the last return statement won’t be executed because return stops the function from executing any further. Perhaps put that console.log before the return statement?

Or you could call the function and store the result in a variable and then console.log that variable.

I’ve put it just below the push statement and still dont get any output


function rangeOfNumbers(startNum, endNum) {
  if (startNum == endNum) {
return [];
} else {
const countArray = rangeOfNumbers(endNum - 1);
countArray.push(endNum);
console.log(countArray);
return countArray;

You’ve got another issue here. The function rangeOfNumbers takes two numbers, startNum and endNum. But you are only passing in one number for the recursive call.

alight i got output with the following function but if im subtracting from endNum why is endNum increasing its super confusing

here is the output for 1,5:
[ 6 ]
[ 6, 7 ]
[ 6, 7, 8 ]
[ 6, 7, 8, 9 ]
[ 6, 7, 8, 9, 10 ]

function rangeOfNumbers(startNum, endNum) {
  if (startNum == endNum) {
return [];
} else {
let countArray = rangeOfNumbers(startNum, endNum - 1);
countArray.push(endNum);
console.log(countArray);
return countArray;

}
  
}

That is not the output for rangeOfNumbers(1,5). As your function is written right now, the output would be:

[ 2 ]
[ 2, 3 ]
[ 2, 3, 4 ]
[ 2, 3, 4, 5 ]
[ 2, 3, 4, 5 ]

Which is a sign that you don’t quite have the correct solution since you are missing 1 at the beginning of the array. I would look at your base case.

You’re subtracting from endNum when you make the recursive function call. But you aren’t subtracting from endNum on the next line when you do

countArray.push(endNum);

So if you call the function as:

rangeOfNumbers(1,5)

Then you will immediately hit the else statement and endNum will be 5 and after you do the recursive call you will then push 5 onto countArray. But you don’t make this push until the recursive call completes and returns a value. And that doesn’t happen until all the other number have been processed by recursive calls. So by the time you get to the original else statement (with endNum equal to 5) the value of countArray will be [1, 2, 3, 4].

Remember, your console.log is executing after you make the recursive call and do the push to countArray. So you won’t see the first console.log until you hit the base case. Each time you hit the else statement the first thing you do is make another recursive call. So each else statement is waiting for the recursive call to return a value before it can execute the lines below the recursive call. What you are seeing printed out is the recursive calls finally being able to finish and return countArray.

here is the copy of the solution it is the same code except for the initial == statement which i changed to match theirs but it still doesnt work this really makes no sense


function rangeOfNumbers(startNum, endNum) {
  if (endNum < startNum) {
    return [];
  } else {
    const numbers = rangeOfNumbers(startNum, endNum - 1);
    numbers.push(endNum);
    return numbers;
  }
}

Are you sure about that?

My advice to you would be to write out what happens on a sheet of paper when you call this function. Start with a very simple example, such as

rangeOfNumbers(1,2);

You know what the answer is going to be. The important part is that you understand how the function calculates it.

I’ll get you started. When you execute the above function call, you will hit the else statement because endNum is greater than startNum. So what is the first thing that is going to happen? It’s going to make a recursive call and set numbers to the return value of that call. So what exactly is that recursive call? Replace the variable names with actual numbers.

I went through it and its confusing i originally but i think i see it. I put startNum into my first return before and it didnt work. I just put endNum in and it works. I retried putting startNum in and it works so maybe i forgot the capital. Also with the empty string it seems like the bottom tests should still pass but they don’t. Also its weird i copied the solution in and it doesnt seem to work.


function rangeOfNumbers(startNum, endNum) {
  if (startNum == endNum) {
return [startNum];
} else {
let array = rangeOfNumbers(startNum, endNum - 1);
array.push(endNum);
console.log(array);
return array;

}
  
}

Also thanks for the help recursion definitly is a bit confusing to me but i am starting to see it more clearly.

Do you mean empty array? Since your base case is using startNum == endNum then returning an empty array won’t work because you will never add that number to the array.

This is another reason why you should write the steps out on paper to make sure you understand why one way does work and the other doesn’t. It’s not enough to get the right code in this case. It is more important to understand why the code works.

Yeah but i still thought that the tests would pass but wasnt thinking about it stopping early. Im definitely going to write stuff out more. The console.log also helped me to understand what was going on in the function so ill use it more in the future. Thanks again for the help.

I’ll get you started. When you execute the above function call, you will hit the else statement because endNum is greater than startNum .