Using javascript, can't pass test case, why?


  • 3
    Z

    The code can pass in development, however it is failed on leetcode. I don't know what's wrong ? the code is below:

     /**
         * @constructor
         * @param {NestedInteger[]} nestedList
         */
        var NestedIterator = function(nestedList) {
            this.list = nestedList;
            this.position = 0;
          this.childIterator = null;
          this.finalList = [];
        };
        
        
        /**
         * @this NestedIterator
         * @returns {boolean}
         */
        NestedIterator.prototype.hasNext = function() {
          return (this.list.length - 1 >= this.position) ? true : false;
        };
        
        /**
         * @this NestedIterator
         * @returns {integer}
         */
        NestedIterator.prototype.next = function() {
            while (this.hasNext()) {
            var item = this.list[this.position];
            if (Object.prototype.toString.call(item) === '[object Array]') {
              var childIterator = this.childIterator;
              if (!childIterator) {
                this.childIterator = childIterator = new NestedIterator(item);
              }
              if (childIterator.hasNext()) {
                var val = childIterator.next();
                if (!childIterator.hasNext()) {
                  this.position++;
                  this.childIterator = null;
                }
                return val;
              }
            } else {
              this.position++;
              return item;
            }
          }
          return this.finalList;
        };

  • 2
    H

    we are in the same boat, I think it's the issue of the problem when using javascript.


  • 0
    H

    Not sure if it's the same issue for you, but my console logs are showing that the nested arrays are not being taken as Array objects. I run an 'Array.isArray(nestedArray)' and it returns false. Strange


  • 0
    D

    Having the same problem too. Works in development, but doesn't pass...

    class NestedIterator {
      constructor(nestedList) {
        this.list = this.flatten(nestedList);
      }
    
      flatten(arr) {
        return [...arr].reduce((a, b) => {
          return a.concat(Array.isArray(b) ? this.flatten(b) : b);
        }, []);
      }
      
      hasNext() {
        return this.list.length !== 0;
      }
    
      next() {
        if (this.hasNext()) return this.list.shift();
        else return null;
      }
    }
    
    let list = new NestedIterator([[1,1],2,[1,1]]);
    list.next(); //=> 1
    list.next(); //=> 1
    list.next(); //=> 2
    list.next(); //=> 1
    list.next(); //=> 1
    

  • 0
    L

    @davidRa said in Using javascript, can't pass test case, why?:

    nestedList

    All the items in the nestedList are "NestedInteger", not a regular Number or Array. And you need to call isInteger(), getInteger(), and getList() instead of using it directly as regular integer or array.


  • 0
    C

    @davidRa

    The problem is in the reduce function. This question is a bit different from plain array recursive flattening.

    You must use the provided isInteger() and getList() functions.

    Here is mine, which passes OK.

    function flatten(arr) {
      return arr.reduce(function (flat, toFlatten) {
        return flat.concat(!toFlatten.isInteger() ? flatten(toFlatten.getList()) : toFlatten);
      }, []);
    

  • 0
    4

    @davidRa said in Using javascript, can't pass test case, why?:

    doesn't

    Have the same problem. The reason is that the input data is list of NestedInteger, not [[1,1],2,[1,1]]


Log in to reply
 

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