Question 200: Major issues with submitting (a correct) javascript solution.
247

Hi all, new to Leetcode and just completed my first solution on a Friday night (well, now Saturday morning :)).

I'm getting the following error whenever I try to submit a Javascript solution. I tested the solution in the Leetcode code editor with many different trials, and seem to always get the correct answer each time. However, whenever I try to submit my final solution, I get a runtime error:

Line 5: TypeError: Cannot read property 'length' of undefined

This comes from trying to get the length of the 2nd dimension of a 2-d array, as follows:

     var numIslands = function(grid) {
		 var TotalNumberFound = 0;
		 var rowCount = grid.length;
		 var colCount = grid[0].length;
		 ...
		 };

When I try to do the grid[0].length, that's where the error is thrown. All I'm trying to do is get the length of the second dimension of the input, which is formatted as follows:

[["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]

I'm expecting a result of simply 5... and it doesn't complain at all about the grid.length on line 4. It only complains when doing grid[0].length.

I'm stumped as to why this one won't go through, and I have a feeling it's just due to my newbieness at formatting the submission properly for Leetcode. Can anyone help me out? I've been trying for a few hours now...

Comments (1)