var arr = [];
/* I thought what if somehow we combine the conditions of if and else if(where startNum = endNum), then I thought about the logic and applied that. As a result, all the test cases were successful.*/
function rangeOfNumbers(startNum, endNum) {
if(startNum <= endNum){
arr.push(startNum);
rangeOfNumbers(startNum + 1, endNum);
return arr;
}
else {
return "invalid input";
}
};
Do you have a question?
Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.
Example:
var myGlobal = [1];
function returnGlobal(arg) {
myGlobal.push(arg);
return myGlobal;
} // unreliable - array gets longer each time the function is run
function returnLocal(arg) {
var myLocal = [1];
myLocal.push(arg);
return myLocal;
} // reliable - always returns an array of length 2
There are actually a few problems with this code.
- You should avoid using a global variable for this problem. But if you do, then the function needs to be defined accordingly. Specifically, the global variable arr is available every way, so there’s no need to return arr in the then part.
if(startNum <= endNum){
arr.push(startNum);
rangeOfNumbers(startNum + 1, endNum);
return arr;
If this function relies on the global variable to store the answer, don’t return anything from the function.
2. If a function is returning a value, then the function call should match it. For example, if a function returns a number, then we should be calling it like
const result = fun(34);
not
fun(34);
So this code
rangeOfNumbers(startNum + 1, endNum);
return arr;
is inconsistent. Here you’re returning arr but you’re calling the function and not assigning the result.
3. The else part
else {
return "invalid input";
}
You will be always end up in returning “invalid input” no matter what. But I don’t think that was your intention. If you want your recursive function to handle the errors in arguments, then you should test it at the top level, not at the end of the recursion.
Your code as written did not “fail” simply because you’re ignoring the return value, but that is not a right way.
Here’s the right way.
a. Don’t use any global
b. Make the function return an array and call it like
const result = rangeOfNumbers(3, 7);
Thanks,
1)I will keep that in mind, I was just experimenting with the code before declaring global variable,So, I forgot about return .
2)Actually, the function call was pre-written in fcc editor, but when practicing by myself I always call function the same way as you stated,like this
const result = rangeOfNumbers(3, 7);
Can you tell me what const do? Because I didn’t reach const part till now. That’s why I use var for function call.
3)Is it good practice to call the recursive function at the end?
With ES6, we have let and const instead of var. We use const when we do not reassign a variable. When we need to change it, we use let.
let x = 10;
...
x = 20; //okay
const y = 30;
. . .
y = 40; //ERROR, you cannot change the value of const
But be careful when const is referring to an array or an object.
const s = [10, 20, 30];
...
s.push(40); //OKAY, we're modifying the content (array) not a variable
...
s = []; //ERROR, can't change s to refer to a different array
Recursive or otherwise, I always place function calls after the function definition.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.