To make a long story short:
Code:
$(function() {
function game_grid(a, b, c) { //Function to create the objects
this.contents = a;
this.size = b;
this.win_length = c;
}
$('#g_go').click(function() { //On click the button
var input = $('#g_input').val(); //Get the input
input = input.split('\n'); //Split by new line
var num_lines = input.length; //Number of lines
var grid_array = []; //array to hold the object for each grid
var i = -1, counter = 0;
while(++i < num_lines) { //Loop through lines
$('#g_g').append(i + '~ ');
var grid_data = input[i].split(' '); //Holds the N, K variables if
if(grid_data.length === 2 && i + +grid_data[0] < num_lines){ //its length is 2, if its N is represented in the input, then
grid_array[counter] = new game_grid(input.slice(++i, i = i + +grid_data[0]), grid_data[0], grid_data[1]);
--i; //set i back one so incrementing it in the while will be the next line
$('body').append(grid_array[counter].size + '~~~ <br />' + grid_array[counter].contents.join('<br>') + '<br> ------- <br />');
++counter; //advance counter for the nest grid, if any
}
}
});
});
For details, read the following and following posts:
First off, it didn't do anything, that's in Chrome and the Fox, until I changed:
Code:
var input = $('#g_input').text(); //Get the input
to (note: .text() will work in IE here, all others appear to require the more precise .val() method):
Code:
var input = $('#g_input').val(); //Get the input
After that there was no error (IE 9+ was the same, with text or val) and the output was like you said.
If then I changed:
Code:
var num_grids = input[counter];
to what seemed more logical to me:
Code:
var num_grids = input.length;
Then I got your exact error. If I then went on to change:
Code:
var grid_data = input[counter].split(' '); //Holds the N, K variables
to:
Code:
var grid_data = input[counter]? input[counter].split(' ') : 'Out of Range'; //Holds the N, K variables
The error went away and I got a lot more output. That was in Chrome only at that point.
I used 'Out of Range' because that's exactly what is happening there, we start at 0 for the array, but counter is already at 1. By the time we get to the end, counter is out of range.
Bookmarks