0

Trying to port my sudoku solving algorithm (which now works!) to javascript, and trying to retrieve the initial values from a series of dropdowns on a page. The basic format of the dropdowns is as follows:

<form action="">
    <table>
        <tr>
            <td>
                <select id="sudoku00">
                    <option value=0></option>
                    <option value=1>1</option>
                    ...
                </select>
            </td>
            ...
        </tr>
        ...
    </table>
</form>

The javascript I'm using to try to retrieve these to an array is as follows, but doesn't seem to work:

var grid=[[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]...]
for (var i=0; i<9; i++){
    for (var j=0; j<9; j++){
        var current=document.getElementById("sudokuCell"+i.string+j.string);
        grid[i][j]=current.options[current.selectedIndex].value;
    }
}

It ends up setting the first cell to undefined and the rest to 0 when none of the dropdowns are changed from blank, and each entry in the smaller arrays becomes undefined when I change the dropdowns to anything other than blank. Also if any of this is bad form please let me know, I'm still pretty new to javascript.

Edit: here's the whole thing. Sorry about the bad id, was typing from memory and missed that. http://jsfiddle.net/2Me7E/

2
  • I personally like suduko solvers just because it reminds me of an N-Queens problem. :) On another note, you seem to have issues with: string needing to be string() and make sure all the select IDs are correct. If you would like i could make you a fiddle Commented May 23, 2014 at 18:32
  • i.string isn't going to do anything unless you define a string() function on the variable. If you're thinking you need to convert the number in i to a string like in Java or C#, where you'd use ToString(), then you don't have to do that in JS. JS will automatically convert the number if you're concatenating it with a string. Commented May 23, 2014 at 18:33

2 Answers 2

1

i.string should be i.toString() and your selector is looking for id sudokuCellxx when your select list's id is sudokuxx.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is an answer i just wrote up in about 4 minutes.

http://jsfiddle.net/69b5p/

//make the grid
var $t = $("<table />"),
    $r = $("<tr />"),
    $c = $("<td />"),
    $sel = $("<select />"),
    $opt = $("<option />"),
    result = $t.clone(),
    selection = $sel.clone();
for (var i = 0; i<9; i++){
    selection.append($opt.clone().val(i+1).text(i+1));
}
for (var i = 9; i; i--){
    var row = $r.clone();
    for (var j = 9; j;j--){
        row.append($c.clone().append(selection.clone()));
    }
    result.append(row);
}


$("body").append(result);

and then you can just add some css:

table, tr, td{border: solid 1px black;}

Edit: Adjusted this fiddle (http://jsfiddle.net/X4fF3/) such that your ID's are assigned AND wraps it with a form.

Edit 2: http://jsfiddle.net/X4fF3/1/ gives an array of all the numbers in order left to right.

With the second edit you can easily find everything which belongs where, by doing something like:

9*R + C%9 where R is the row number and C is the column. (this is programming so it would actually be 0-8 for each of them. :)

example:

function get(R,C){ return array[9*R + C%9]; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.