# Porting RDSE to JavaScript

**URL:** <https://discourse.numenta.org/t/porting-rdse-to-javascript/679>\
**Category:** Engineering\
**Created:** [June 1, 2016, 4:00pm UTC](https://discourse.numenta.org/t/porting-rdse-to-javascript/679 "2016-06-01T16:00:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [June 1, 2016, 4:00pm UTC](https://discourse.numenta.org/t/porting-rdse-to-javascript/679/1 "2016-06-01T16:00:54Z")

</div>

I’m running into a problem porting the RDSE from [python](https://github.com/numenta/nupic/blob/master/src/nupic/encoders/random_distributed_scalar.py#L40) to [JavaScript](https://github.com/htm-community/sdr-viz/blob/master/js/lib/encoders.js#L8). Looking for help understanding why my `_createBuckets` function is recursively diving into negative indices:

[https://github.com/htm-community/sdr-viz/blob/master/js/lib/encoders.js#L164-L191](https://github.com/htm-community/sdr-viz/blob/master/js/lib/encoders.js#L164-L191)

You can see this in action live at [http://htm-community.github.io/sdr-viz/site/rdse.html](http://htm-community.github.io/sdr-viz/site/rdse.html) (your browser might hang because `_createBucket` is a recursive function and blows the call stack. But if you open up a web inspector, you should see the following dump to the console:

```auto
Adding additional buckets to handle index=500
encoders.js:167 createBucket(500) (should not be negative)
encoders.js:167 createBucket(499) (should not be negative)
encoders.js:167 createBucket(500) (should not be negative)
encoders.js:167 createBucket(499) (should not be negative)
encoders.js:167 createBucket(498) (should not be negative)
encoders.js:167 createBucket(499) (should not be negative)
...
encoders.js:167 createBucket(4) (should not be negative)
encoders.js:167 createBucket(3) (should not be negative)
encoders.js:167 createBucket(2) (should not be negative)
encoders.js:167 createBucket(3) (should not be negative)
encoders.js:167 createBucket(2) (should not be negative)
encoders.js:167 createBucket(1) (should not be negative)
encoders.js:167 createBucket(2) (should not be negative)
encoders.js:167 createBucket(1) (should not be negative)
encoders.js:167 createBucket(0) (should not be negative)
encoders.js:167 createBucket(1) (should not be negative)
encoders.js:167 createBucket(0) (should not be negative)
encoders.js:167 createBucket(-1) (should not be negative)
encoders.js:167 createBucket(0) (should not be negative)
encoders.js:167 createBucket(-1) (should not be negative)
encoders.js:167 createBucket(-2) (should not be negative)
encoders.js:167 createBucket(-1) (should not be negative)
encoders.js:167 createBucket(-2) (should not be negative)

```

… eventually leading to a `RangeError: Maximum call stack size exceeded` exception.

From the docs in RDSE:

> <https://github.com/numenta/nupic/blob/master/src/nupic/encoders/random_distributed_scalar.py#L409-L413>

The **_"as long as they don’t become negative"_** part seems pretty important. I assume this is related to my bug. But I don’t see any check in the python RDSE to prevent negative indices. Can anyone help me understand what I’ve done wrong?

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [June 1, 2016, 4:43pm UTC](https://discourse.numenta.org/t/porting-rdse-to-javascript/679/2 "2016-06-01T16:43:00Z")

</div>

I found my problem, it was completely unrelated to the recursive function above:

> <https://github.com/htm-community/htm-school-viz/commit/4c0586920fdf20edaa925e588c775a1c49848f1c#diff-05b5c085d7a998ad8d860533031b714eL202>

The lodash library I’m using was returning all string keys from an object when calling `.keys()` on it, even if the keys were not actually strings.
