Tell us what’s happening:
for what the console.log() function returns my code is working and giving the right answer but for some reason when I run the test it tells me that it’s wrong. anyone know why this is happening? I don’t know if it’s for a mistake on my code that i"m not seeing or if there is a probleme with the page.
sorry for writing mistakes english is not my first language.
Your code so far
const vector = [];
function rangeOfNumbers(startNum, endNum) {
/*
for (var i = startNum; i <= endNum; i++){
vector.push(i);
}
return vector;
*/
var i = startNum;
if (i == endNum){
vector.push(i)
} else{
vector.push(i)
rangeOfNumbers(startNum+1, endNum)
}
return(vector);
};
console.log(rangeOfNumbers(1, 5));
//console.log(rangeOfNumbers(6, 9));
//console.log(rangeOfNumbers(4, 4));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.
Challenge: Use Recursion to Create a Range of Numbers
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
thanks, but any tip on how to avoid the variable to reset each time?
This way if I use recursion it resets every time and it only shows the last value.
you could do it without a variable, or you could do it with a variable (local! can’t be global)
it depends on the method of your choice
if it works, it works