Tell us what’s happening:
Ended up blocking myself, i’m stuck,don’t know how to resolve this challenge
Your code so far
function rangeOfNumbers(startNum, endNum) {
if(endnum - startNum === 0){
var range = [];
range.push(startNum);
return range;
}else{
range = rangeOfNumbers(startNum, endNum - 1);
range.push(endNum);
return range
}
};
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0.
Challenge: Use Recursion to Create a Range of Numbers
Link to the challenge:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
What problems are you having? What else have you tried?
in the tests, it doesn’ t return an array, neither any of the expected numbers
If you look at your console, you’ll probably see something like ReferenceError: endnum is not defined. Does that help?
whats your letter casings
/ running tests Your function should return an array.
rangeOfNumbers(1, 5)
should return
[1, 2, 3, 4, 5]
.
rangeOfNumbers(6, 9)
should return
[6, 7, 8, 9]
.
rangeOfNumbers(4, 4)
should return
[4]
. // tests completed
there is no reference error in the console, only this
As the console says, you referencing an undefined variable:
endnum.
1 Like
I suck at recursion.
But I took it step by step. Adding “features” to the function and console out after each step. Maybe it helps understanding by watching the commented out code I wrote, and seeing my “progress” to the final solution:
// function some(x) {
// if(x === 0) {
// return;
// }
// some(x - 1);
// console.log(x);
// }
// some(5);
// function some(start, x) {
// if(x === start) {
// console.log(x)
// return;
// }
// some(start, x - 1);
// console.log(x);
// }
// some(2, 5);
// let array = [];
// function some(start, x) {
// if(x === start) {
// // console.log(x);
// array.push(x);
// return;
// }
// some(start, x - 1);
// // console.log(x);
// array.push(x);
// }
// some(2,5);
// console.log(array);
// let array = [];
// function some(array, start, x) {
// if(x === start) {
// // console.log(x);
// array.push(x);
// return;
// }
// some(array, start, x - 1);
// // console.log(x);
// array.push(x);
// }
// some(array, 2, 5);
// console.log(array);
function some(start, x) {
if(x === start) {
// console.log(x);
let array = []
array.push(x);
return array;
}
let array = some(start, x - 1);
// console.log(x);
array.push(x);
return array;
}
let array = some(2, 2);
console.log(array);