8000 [ADD] rangeOfNumbers added · luisprooc/js-algorithms@7826341 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7826341

Browse files
committed
[ADD] rangeOfNumbers added
1 parent 2f81121 commit 7826341

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,22 @@ bob.getLastName() should return the string Curry after bob.setFullName("Haskell
220220
```
221221

222222

223+
## Range of Numbers Recursive
224+
225+
We have defined a function named rangeOfNumbers with two parameters. The function should return an array of integers which begins with a number represented by the startNum parameter and ends with a number represented by the endNum parameter. The starting number will always be less than or equal to the ending number. Your function must use recursion by calling itself and not use loops of any kind. It should also work for cases where both startNum and endNum are the same.
226+
227+
228+
```javascript
229+
Your function should return an array.
230+
231+
Your code should not use any loop syntax (for or while or higher order functions such as forEach, map, filter, or reduce).
232+
233+
rangeOfNumbers should use recursion (call itself) to solve this challenge.
234+
235+
rangeOfNumbers(1, 5) should return [1, 2, 3, 4, 5].
236+
237+
rangeOfNumbers(6, 9) should return [6, 7, 8, 9].
238+
239+
rangeOfNumbers(4, 4) should return [4].
240+
```
241+

src/rangeOfNumbers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function rangeOfNumbers(startNum, endNum) {
2+
if(startNum > endNum) return [];
3+
4+
else{
5+
const myArr = rangeOfNumbers(startNum + 1, endNum);
6+
myArr.unshift(startNum);
7+
return myArr;
8+
}
9+
};
10+
11+
console.log(rangeOfNumbers(1,5));

0 commit comments

Comments
 (0)
0