# Top down javascript solution

• Here's my solution in javascript, the recursive portion can be cleaned up for sure, let me know what you guys think!

``````/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* function NestedInteger() {
*
*     Return true if this NestedInteger holds a single integer, rather than a nested list.
*     @return {boolean}
*     this.isInteger = function() {
*         ...
*     };
*
*     Return the single integer that this NestedInteger holds, if it holds a single integer
*     Return null if this NestedInteger holds a nested list
*     @return {integer}
*     this.getInteger = function() {
*         ...
*     };
*
*     Set this NestedInteger to hold a single integer equal to value.
*     @return {void}
*     this.setInteger = function(value) {
*         ...
*     };
*
*     Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
*     @return {void}
*         ...
*     };
*
*     Return the nested list that this NestedInteger holds, if it holds a nested list
*     Return null if this NestedInteger holds a single integer
*     @return {NestedInteger[]}
*     this.getList = function() {
*         ...
*     };
* };
*/
/**
* @param {string} s
* @return {NestedInteger}
*/
var deserialize = function(s) {
if (s[0] !== '[') {
return parseInt(s, 10);
}

var internals = s.slice(1, s.length - 1);
var firstBracket = internals.indexOf('[');
var nestedInt = new NestedInteger();
var currentNum = '';
var i = 0;

while (i < internals.length) {
if (internals[i] === ',') {
currentNum = '';
i++;
} else if (internals[i] === '[') {
// find closing index
var closingIndex = findIndexOfClosing(internals, i);
var deserialized = deserialize(internals.slice(i, closingIndex + 1));

if (typeof deserialized === 'number') {
// if nestedInt is list then we add to the list
if (nestedInt.getList()) {
} else {
nestedInt.setInteger(deserialized);
}
} else { // deserialized is a nestedInt
var deserializedList = deserialized.getList();
var deserializeInteger = deserialized.getInteger();

if (deserializedList) {
} else if (deserializeInteger) {
}
}

i = closingIndex + 1;
if (internals[i] === ',') {
i++;
}
currentNum = '';
} else {
currentNum += internals[i];
i++;
}
}

if (currentNum) {
if (nestedInt.getList()) {
} else {
nestedInt.setInteger(parseInt(currentNum, 10));
}
}

return nestedInt;
};

var findIndexOfClosing = function(s, start) {
var opens = 0;

for (var i = start; i < s.length; i++) {
if (s[i] === '[') {
opens++;
} else if (s[i] === ']') {
opens--;
}

if (opens === 0) {
return i;
}
}

return -1;
};`````````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.