The Script below generates a sudoku (9 by 9) table and displays the table on screen.

what I am trying to do is to get a string of numbers entered into the text field labeled "enter values" on the page below the table, into each cell box of the sudoku table when the button labelled "load" is pressed. This would fill the sudoku table with characters, one character (from the text field) to each of cell boxes in the sudoku table.

It requires using a for loop, I tried using one in my function (g) but it doesn't work,... I really need your help on this one,...

_____________________________________________________

<html>
<head>
<title>Sudoku</title>

 <style type="text/css">
     body          { background: gold; color: maroon}
     .center       { text-align: center }
     td.changed    { color: red }
  </style>


<script type='text/javascript'>
//Script that generates sudoku table

function f(){
var s="<table border='5'>";
var k=0;
    for (var b=0; b<3; b++) {
    s+="<tr>";
        for (var a=0; a<3; a++) {
            s+="<td><table border='5'>";

                for (var i=0; i<3; i++) {
                s+="<tr>";
                    for (var j=0; j<3; j++) {
                    s+="<td><input type='text' id=' cell: "+b+""+a+""+i+""+j+"' size='1' class='bigcol'</td>";
                    k++;
                    }
                s+= "</tr>";
                }
            s+= "</table></td>";
            }
        s+= "</tr>";
        }
    s+="</table>";
    document.getElementById('sudoku').
    innerHTML=s;
    }
</script>

<script type='text/javascript'>
// attempt to initialize all the cell boxes in the sudoku table, that is to put values into table to make it playable
function g(){
var s = char.value;
var k = 0;
for (var s=0; s<5; s++) {
    s.charAt(k)=document.getElementById('"+b+","+a+","+i+","+j+"').value; 
    k++;
    }
}
</script>

</head>


<body onLoad="f();">

<form name='form1' class="center">
<div id="sudoku" align="center"></div>
<br/>

<p><b>Enter values</b> : <input type='text' size=81 id='char' />
<input type='button' value='Load' onclick='g()' /></p>
<input type="button" value="Submit" onClick="submit();"/>
<input type="button" value="Reload Page" onClick="window.location.reload()">

</form>
</body>
</html>

Mstrlouis,

There are several problems with the function g()

  • The symbol s is reused (try using k as the loop counter).
  • In the line s = char.value , char is not defined (it may also be a reserved word in javascript? - best avoided).
  • s.charAt(k) = .... will give the javascript error - "Cannot assign a function to a result".
  • The varaibles b , a , i , j , as defined in f() , are out of scope. If g() is called from f() then you need to pass b , a , i , j as arguments or, even better, concetenate them in f() and pass the conacatenated string as a single argument to g() .

If you want to build a string, then start with eg. str = "" , and iteratively add to it in a loop with str += ..... .

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.