How do i store data with a Two Dimensional Array? Please help!!
Basically im trying to create a sudoku page, and the user inputs the numbers they want to into empty table cells and i want them to be able to save what they have done by clicking on a button. Ive got a two dimensional array but im not sure if it works because i dont know how to link the array with the button :/ im only just starting javscript at uni so im just a beginner. please help!
var current_cell = null; // the currently selected cell
var saved = {}; // Object for saving the current game
function initialize() {
var col, row;
// Work through all the cells in the table and set
// onclick event handlers and classNames for the empty
// ones.
for (row = 0; row <=8; row++) {
for (col=0; col <= 8; col++) {
var cell = document.getElementById('cell_' + col + '_' + row);
if (!parseInt(cell.innerHTML)) {
// cell is empty
cell.onclick = selectCell;
cell.className = 'tofill';
}
}
}
document.onkeypress = keyPress;
save();
}
var data = new array(9)
for (i=0; i <9 ; i++)
data[i] = new array(9)
I don't see why you would want to attack a two dimension array if you are a beginner.
Here is a way that a single dimension array could be used (a lot easier to understand).
The script does NOT play the game or even set-up a valid game.
All it does is show how a single array can be displayed in two dimensions.
BTW: You should enclose you scripts between [ code] and [ /code] tags (without the spaces)
to make it easier to read, copy, test and debug your attempts.
The simpler form to describe a Sudoku is probably a string like this :
'000000012000035000000600070700000300000400800100000000000120000080000040050000600'
Where 0 represent the lear cases...
Paste this string in this Sudoku page to see an example of Sudoku game...
Thanks guys, much appreciated unfortunately i was told i have to use two dimensional arrays by my lecturer and that is why i have been struggling because it is quite confusing. I didnt need to draw the table as it had already been drawn in html i only had to create a save button so what the user enters into the cells would be saved.. but i cant find an exact way to do that :/
Thanks guys, much appreciated unfortunately i was told i have to use two dimensional arrays by my lecturer and that is why i have been struggling because it is quite confusing. I didnt need to draw the table as it had already been drawn in html i only had to create a save button so what the user enters into the cells would be saved.. but i cant find an exact way to do that :/
There is not a way to save the user results using JS only.
The closest you can come is to use a cookie or if your browser supports it, local storage.
Your original post was incomplete, so I used the two dimensions (r and c) to write to it on the screen.
You can use a similar logic to save the results to an array rather than the individual <div> elements.
But again, it will be temporary storage only, ie; until you exit the page.
A computer memory is an unidimensional array with addresses and values. Then it is always better to work with keys (from 0 to 80 for Sudokus) to divide this keys by 9 to find the quotient (which give the row from 0 to 8) and the rest (key%9 which give the column).
Bookmarks