# JavaScript Getting range numbers using recursion in JavaScript

I am trying to get the range of numbers using recursion. Can someone explain to me why it isn't working?

``````function range(x,y){
var results = [];
if(x === y){
return results;
}

return  results.push(range(x + 1,y));
}

range(1,5);
``````

The beauty of recursion is that you don't need local variables (`var results`). You just pass state as arguments to each recursive iteration:

``````const concat = (xs, y) => xs.concat(y);

const range = (x, y) => {
const rec = (x, y, acc) => x < y ? rec(x + 1, y, concat(acc, x)) : acc;
return rec(x, y, []);
}
``````

ES5 version in case you aren't familiar with the arrow syntax:

``````function concat(xs, y) {
return xs.concat(y);
}

function range(x, y) {
function rec(x, y, acc) {
return x < y ? rec(x + 1, y, concat(acc, x)) : acc;
}

return rec(x, y, []);
}
``````

That isn't the most elegant solution though!

With recursion we can simply build up the stack with each recursive call. Each stack frame contains a computed partial result. Then we just need to unwind the stack and attach each partial result to an array:

``````const range = (x, y) => x < y ? [x].concat(range(x + 1, y)) : [];
``````

Or more functional:

``````const concat = (xs, y) => xs.concat(y);
const range = (x, y) => x < y ? concat([x], range(x + 1, y)) : [];
``````

Note that `concat([x], range(x + 1, y))` is the recursive case and `[]` the base case.

Try this:

``````function rangeOfNumbers(startNum, endNum) {
if (startNum - endNum === 0) {
return [startNum];
} else {
const numbers = rangeOfNumbers(startNum + 1, endNum);
numbers.unshift(startNum);
return numbers;
}
};
``````

## Data is not going through the ajax code

I'm using Facebook login feature & transferring some variables to other page through ajax. This particular ajax code isn't working, data is not going through. However I've other ajax code in other ...

I'm using Facebook login feature & transferring some variables to other page through ajax. This particular ajax code isn't working, data is not going through. However I've other ajax code in other ...

1. going through data too fast
2. going through data synonym
3. going through data

## Check if property name is array index

I want to assign some properties to an array, but only if they are array indices. Otherwise some implementations might switch the underlying structure to a hash table and I don't want that. For ...

I want to assign some properties to an array, but only if they are array indices. Otherwise some implementations might switch the underlying structure to a hash table and I don't want that. For ...

1. check property name javascript
2. check property name
3. check property name c#
4. check property name js
5. check property owner name
6. check if property name exists javascript
7. check object property name
8. check property tax by name
9. check property ownership by name
10. property name check online
11. c# check if property name exists
12. javascript check object property name

## Loopback Strongloop get ModelName from model instance

Using loopback I need to get the modelName from a model instance. Any Ideas? var myAccount = new app.models.Account({name: 'ACME'}); var modelName = myAccount.modelName;` //should get 'Account'

Using loopback I need to get the modelName from a model instance. Any Ideas? var myAccount = new app.models.Account({name: 'ACME'}); var modelName = myAccount.modelName;` //should get 'Account'

## How do I get features from a geoJson load?

I've got some GeoJson loading into an openlayers 3 vector layer var countriesLayer = new ol.layer.Vector({ source: new ol.source.Vector({ url: '/data/countriesandstates.geojson', format: ...

I've got some GeoJson loading into an openlayers 3 vector layer var countriesLayer = new ol.layer.Vector({ source: new ol.source.Vector({ url: '/data/countriesandstates.geojson', format: ...

1. features geojson
2. features geojson format
3. features geojson polygon
4. openlayers add features from geojson
1