## Challenge #11 - error, but none in inspector?!


#1

So this code works in the console tab of chrome’s inspector, but throws this error when used in the challenge!

Your code could not run: “Cannot read property ‘3’ of undefined”

function lightColumn(column) {
var result = [];
var col = convertColumn(column);
if (col >= GRID[0].length || col < 0) {
return “Column outside of GRID”;
} else {
for (var i = 0; i < GRID[0].length; i++) {
result.push(GRID[i][col]);
}
return result;
}
}

can somebody help me out here? :slight_smile:


Challenge 11 - Cannot read property '3' of undefined
#2

I haven’t tested your code, but at a glance this is the only bit that looks odd for me. If your column is J, does it return “Column outside of GRID”?


#3

It doesn’t!

lightColumn(‘J’);
(10) ["", “”, “”, “”, “”, “”, “”, “”, “”, “”]

lightColumn(‘K’);
“Column outside of GRID”

That was run in chrome’s inspector of course :smiley:

Here is my full code if you want to play with it:

Full Code

function countRows() {
return GRID.length;
}
function countColumns() {
return GRID[0].length;
}
function gridSize() {
var rows;
var cols;
var grid;

rows = countRows();
cols = countColumns();
grid = cols + " x " + rows;

return grid;
}
function totalCells() {
var rows;
var cols;
var size;

rows = countRows();
cols = countColumns();
size = rows * cols;
return size;
}
function convertColumn(coordinate) {
if (coordinate[0].match(/[A-Z]/)) {
var rtn = coordinate[0].charCodeAt(0) - 65;
return rtn;
} else {
return “Coordinate out of GRID”
}
}
function lightCell(coordinate) {
var xVal;
var yVal;
xVal = convertColumn(coordinate);
yVal = coordinate[1] - 1;
return GRID[yVal][xVal];
}
function isRock(coordinate) {
cell = lightCell(coordinate);
if (cell === “^”) {
return true;
} else {
return false;
}
}
function isCurrent(coordinate) {
if (lightCell(coordinate) === “~”) {
return true;
} else {
return false;
}
}
function isShip(coordinate) {
if (lightCell(coordinate) === “v”) {
return true;
} else {
return false;
}
}
function lightRow(row) {
if (row > GRID.length || row < 1) {
return “Row outside of GRID”;
} else {
row -= 1;
return GRID[row];
}
}
function lightColumn(column) {
var result = [];
var col = convertColumn(column);
if (col >= GRID[0].length || col < 0) {
return “Column outside of GRID”;
} else {
for (var i = 0; i < GRID[0].length; i++) {
result.push(GRID[i][col]);
}
return result;
}
}


#4

Shouldn’t it be i < GRID.length instead of i < GRID[0].length in the loop?


#5

I don’t think so. The GRID array contains an array of rows. If we check the length of GRID, we are checking to see if a row is in the grid. GRID[n] is the list of columns. So GRID[0].length should give us the number of columns and not rows. Correct me if I’m wrong here! :slight_smile:


#6

have you tried to test to see if i < GRID.length works?


#7

Yep! The code still doesn’t pass. Same error. If my assumptions are correct, that will work but could lead to issues if the grid is not square :slight_smile:


#8

I would agree with chongtianzongwu. The for loop would need to be i < GRID.length. If you look at lighting up a column, we are determining what is in each row at a certain column. As an example, to light up column B we need to see what is in the second column over, at each row. When you test ‘i < GRID[0].length’ then if there happened to be another row, your lightColumn function would not capture what is in column B of the last row. I would edit your code as follows to see if it works. Remove the code for testing if the specified column is outside of the grid, because your convertColumn function already does that, then in the for loop test for i<GRID.length, like so:

function lightColumn(column) {
var result = [];
var col = convertColumn(column);
for (var i = 0; i < GRID.length; i++) {
result.push(GRID[i][col]);
}
return result;
}


#9

If that doesn’t work try:

function lightColumn(column) {
var result = [];
var col = column.charCodeAt(0) - 65;
for (var i = 0; i < GRID.length; i++) {
result.push(GRID[i][col]);
}

Technically speaking, the convertColumn function you used is supposed to take in a coordinate (eg. ‘B2’), not a column (eg. ‘B’). But the convertColumn function will spit out the correct column number if only supplied with the column, so it should work either way, but it may depend on how LightHouse has set up its testing parameters.


#10

@paulbeaudon you’re a gem! I’m going to put some time and energy into this tonight. I’ll report back!


#11

Good morning! Strangely, (and I’m sure I have tried this before) changing my for loop condition to i < GRID.length works this morning! I am quite sure I have tried this before but had no luck. I feel like somebody at lighthouse updated something without replying to my other topic for support. Grr. Anyhow!

I wanted to try and DRY my code a bit so I know that the convertColumn function was supposed to take a full coordinate and I was initially just sending a dummy (just a 1 or something) for the row, but I decided to change that function so that it could do it either way so I could rely on it for this.

Thank you @paulbeaudon for explaining my error in looking at GRID[0].length in my code. I can see what was doing wrong now!


#12

No problem, glad to help out. Enjoy the remaining challenges.