My code works perfectly on my puter, on all my browsers (including ie), But I can't manage to make it work on jsFiddle.
The problem seems to be on:
window.onload=function(){
document.body.innerHTML+=brd+log;
}
But I have tried both with and without the window.onload
. I am totally clueless and I am starting to lose all hope
CSS (optional):
*{margin:0;padding:0;}
#brd{margin:15px auto;}
#brd,#brd td,#log{border:1px solid #000;}
#brd td{height:47px;width:45px;text-align:center;font-size:30px;}
#brd span{font-size:12px;}
#log{margin:15px auto;padding:20px;width:385px;max-height:150px;overflow:auto;background:#F0F0F0;color:#000;}
JavaScript:
var i,sdk,rem,log,perf,brd,bxes=[["11","12","13","21","22","23","31","32","33"],["14","15","16","24","25","26","34","35","36"],["17","18","19","27","28","29","37","38","39"],["41","42","43","51","52","53","61","62","63"],["44","45","46","54","55","56","64","65","66"],["47","48","49","57","58","59","67","68","69"],["71","72","73","81","82","83","91","92","93"],["74","75","76","84","85","86","94","95","96"],["77","78","79","87","88","89","97","98","99"]];
/*Array Find*/
function arrFind(fnd,frm,bol){
var ii,len,rtn=-1;
for(ii=0,len=frm.length;ii<len;ii++){
if(frm[ii]==fnd){
rtn=ii;
break;
}
}
return bol?rtn>-1:rtn;
}
/*To RC*/
function toRC(val){
var ii,len,aa=[];
val=val.split(",");
for(ii=0,len=val.length;ii<len;ii++){
aa.push(Math.ceil((parseInt(val[ii])+1)/9)+""+((parseInt(val[ii])%9)+1));
}
return aa.join(",");
}
/*To AV*/
function toAV(rc){
var ii,len,aa=[];
rc=rc.split(",");
for(ii=0,len=rc.length;ii<len;ii++){
aa.push((((parseInt(rc[ii].charAt(0))-1)*9)+(parseInt(rc[ii].charAt(1))-1))+"");
}
return aa.join(",");
}
/*Get Box*/
function getBox(av,bol){
var ii,jj,len,ss,zz,aa=[];
if(!bol){
av=toRC(av);
}
av=av.split(",");
for(ii=0,len=av.length;ii<len;ii++){
ss=av[ii];
for(jj=0;jj<9;jj++){
zz=window.bxes[jj];
if(arrFind(ss,zz,true)){
aa.push(zz);
break;
}
}
}
return aa.join(",");
}
/*Get Row*/
function getRow(av,bol){
var ii,len,ss,aa=[];
if(!bol){
av=toRC(av);
}
av=av.split(",");
for(ii=0,len=av.length;ii<len;ii++){
ss=av[ii].charAt(0);
aa.push(ss+"1",ss+"2",ss+"3",ss+"4",ss+"5",ss+"6",ss+"7",ss+"8",ss+"9");
}
return aa.join(",");
}
/*Get Column*/
function getCol(av,bol){
var ii,len,ss,aa=[];
if(!bol){
av=toRC(av);
}
av=av.split(",");
for(ii=0,len=av.length;ii<len;ii++){
ss=av[ii].charAt(1);
aa.push("1"+ss,"2"+ss,"3"+ss,"4"+ss,"5"+ss,"6"+ss,"7"+ss,"8"+ss,"9"+ss);
}
return aa.join(",");
}
/*Get Value*/
function getVal(av,bol){
var ii,len,ss,aa=[];
if(bol){
av=toAV(av);
}
av=av.split(",");
for(ii=0,len=av.length;ii<len;ii++){
ss=window.sdk[parseInt(av[ii])];
if(ss.length==1){
aa.push(ss);
}
}
return aa.join(",");
}
/*Get Candidates*/
function getCand(av,bol){
var ii,len,ss,zz,aa=["1","2","3","4","5","6","7","8","9"];
if(bol){
av=toAV(av);
}
if(window.sdk[parseInt(av)].length==1){
return [];
}
zz=getVal(getBox(av)+","+getRow(av)+","+getCol(av),true).split(",");
for(ii=0,len=zz.length;ii<len;ii++){
ss=arrFind(zz[ii],aa);
if(ss>-1){
aa.splice(ss,1);
}
}
return aa.join(",");
}
/*New Sudoku*/
function newSudoku(str){
var ii,len,ss;
window.perf=new Date().getTime();
window.log="";
window.sdk=[];
window.rem=[];
for(ii=0,len=str.length;ii<len;ii++){
ss=str.charAt(ii);
if(ss!="0"){
window.log+=ss+" on "+toRC(ii+"")+"<br>";
writeNumber(ss,ii+"");//wn
}else{
window.sdk[ii]=[];
window.rem.push(ii+"");
}
}
}
/*Write Number*/
function writeNumber(val,av,bol){
if(bol){
av=toAV(av);
}
window.sdk[parseInt(av)]=[val];
return delCand(val,av);
}
/*Delete Candidates*/
function delCand(val,av,bol){
var ii,jj,len,jen,ss,zz,cc,xx,xo,dd=[];
if(bol){
av=toAV(av);
}
zz=toRC(av);
xx=new RegExp(","+zz+",","g");//below bol
ss=toAV((getBox(av)+","+getRow(av)+","+getCol(av)).replace(xx,",").replace(zz+",","").replace(","+zz,"")).split(",");
for(ii=0,len=ss.length;ii<len;ii++){
cc=window.sdk[ss[ii]];
if(!cc||cc.length<2){
continue;
}
zz=arrFind(val,cc);
if(zz>-1){
window.sdk[parseInt(ss[ii])].splice(zz,1);
if(window.sdk[parseInt(ss[ii])].length==1){
window.log+=window.sdk[parseInt(ss[ii])][0]+" on "+toRC(ss[ii])+"<br>";
xo=delCand(window.sdk[parseInt(ss[ii])][0],ss[ii]);//wn
jen=xo.length;
if(jen>0){
for(jj=0;jj<jen;jj++){
dd.push(xo[jj]);
}
}
dd.push(ss[ii]);
}
}
}
return dd;
}
/*Delete Remaining*/
function delRem(arr){
var ii,len,ss;
for(ii=0,len=arr.length;ii<len;ii++){
ss=arrFind(arr[ii],window.rem);
if(ss>-1){
window.rem.splice(ss,1);
}
}
}
/*Solve Sudoku*/
function solSudoku(){
var ii,jj,ll,yy,len,jen,ss,zz,cc,dd,xo;
for(ll=0;ll<1;ll++){
dd=[];
for(ii=0,len=window.rem.length;ii<len;ii++){
zz=window.sdk[parseInt(window.rem[ii])];
if(zz.length==0){
ss=getCand(window.rem[ii]).split(",");
if(ss.length==1&&ss[0]!=""){
window.log+=ss[0]+" on "+toRC(window.rem[ii])+"<br>";
xo=writeNumber(ss[0],window.rem[ii]);//wn
jen=xo.length;
if(jen>0){
for(yy=0;yy<jen;yy++){
dd.push(xo[yy]);
}
}
dd.push(window.rem[ii]);
ll=-1;
}else{
if(ss.length>1){
window.sdk[parseInt(window.rem[ii])]=ss;
}
}
}else if(zz.length==1){
alert("solSudoku warning");
dd.push(window.rem[ii]);
}
}
delRem(dd);
}
if(window.rem.length!=0){
//failed to solve by simple solving
//start advanced solving
}
}
//============================================================================
newSudoku("032060007000008290000509000000300016800000002610002000000405000045800000700010530");
solSudoku();
//000124039007093056390000200040708000008050400000201060005000078270980300980517000
//032060007000008290000509000000300016800000002610002000000405000045800000700010530
//delCand("5","55",true);
//alert("["+sdk.join("][")+"]");
log+="--- Time: "+(new Date().getTime()-perf)+" milliseconds ---";
brd="<table id=\"brd\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr>";
for(i=0;i<sdk.length;i++){
if(i%9==0){
if(i!=0&&i!=80){
brd+="</tr><tr>";
}
}
if(sdk[i].length==0){
brd+="<td> </td>";
}
if(sdk[i].length==1){
brd+="<td>"+sdk[i]+"</td>";
}
if(sdk[i].length>1){
brd+="<td><span>"+sdk[i].join(" ")+"</span></td>";
}
}
brd+="</tr></tbody></table>";
log="<div id=\"log\">"+log+"</div>";
window.onload=function(){
document.body.innerHTML+=brd+log;
}
My code works perfectly on my puter, on all my browsers (including ie), But I can't manage to make it work on jsFiddle.
The problem seems to be on:
window.onload=function(){
document.body.innerHTML+=brd+log;
}
But I have tried both with and without the window.onload
. I am totally clueless and I am starting to lose all hope
CSS (optional):
*{margin:0;padding:0;}
#brd{margin:15px auto;}
#brd,#brd td,#log{border:1px solid #000;}
#brd td{height:47px;width:45px;text-align:center;font-size:30px;}
#brd span{font-size:12px;}
#log{margin:15px auto;padding:20px;width:385px;max-height:150px;overflow:auto;background:#F0F0F0;color:#000;}
JavaScript:
var i,sdk,rem,log,perf,brd,bxes=[["11","12","13","21","22","23","31","32","33"],["14","15","16","24","25","26","34","35","36"],["17","18","19","27","28","29","37","38","39"],["41","42","43","51","52","53","61","62","63"],["44","45","46","54","55","56","64","65","66"],["47","48","49","57","58","59","67","68","69"],["71","72","73","81","82","83","91","92","93"],["74","75","76","84","85","86","94","95","96"],["77","78","79","87","88","89","97","98","99"]];
/*Array Find*/
function arrFind(fnd,frm,bol){
var ii,len,rtn=-1;
for(ii=0,len=frm.length;ii<len;ii++){
if(frm[ii]==fnd){
rtn=ii;
break;
}
}
return bol?rtn>-1:rtn;
}
/*To RC*/
function toRC(val){
var ii,len,aa=[];
val=val.split(",");
for(ii=0,len=val.length;ii<len;ii++){
aa.push(Math.ceil((parseInt(val[ii])+1)/9)+""+((parseInt(val[ii])%9)+1));
}
return aa.join(",");
}
/*To AV*/
function toAV(rc){
var ii,len,aa=[];
rc=rc.split(",");
for(ii=0,len=rc.length;ii<len;ii++){
aa.push((((parseInt(rc[ii].charAt(0))-1)*9)+(parseInt(rc[ii].charAt(1))-1))+"");
}
return aa.join(",");
}
/*Get Box*/
function getBox(av,bol){
var ii,jj,len,ss,zz,aa=[];
if(!bol){
av=toRC(av);
}
av=av.split(",");
for(ii=0,len=av.length;ii<len;ii++){
ss=av[ii];
for(jj=0;jj<9;jj++){
zz=window.bxes[jj];
if(arrFind(ss,zz,true)){
aa.push(zz);
break;
}
}
}
return aa.join(",");
}
/*Get Row*/
function getRow(av,bol){
var ii,len,ss,aa=[];
if(!bol){
av=toRC(av);
}
av=av.split(",");
for(ii=0,len=av.length;ii<len;ii++){
ss=av[ii].charAt(0);
aa.push(ss+"1",ss+"2",ss+"3",ss+"4",ss+"5",ss+"6",ss+"7",ss+"8",ss+"9");
}
return aa.join(",");
}
/*Get Column*/
function getCol(av,bol){
var ii,len,ss,aa=[];
if(!bol){
av=toRC(av);
}
av=av.split(",");
for(ii=0,len=av.length;ii<len;ii++){
ss=av[ii].charAt(1);
aa.push("1"+ss,"2"+ss,"3"+ss,"4"+ss,"5"+ss,"6"+ss,"7"+ss,"8"+ss,"9"+ss);
}
return aa.join(",");
}
/*Get Value*/
function getVal(av,bol){
var ii,len,ss,aa=[];
if(bol){
av=toAV(av);
}
av=av.split(",");
for(ii=0,len=av.length;ii<len;ii++){
ss=window.sdk[parseInt(av[ii])];
if(ss.length==1){
aa.push(ss);
}
}
return aa.join(",");
}
/*Get Candidates*/
function getCand(av,bol){
var ii,len,ss,zz,aa=["1","2","3","4","5","6","7","8","9"];
if(bol){
av=toAV(av);
}
if(window.sdk[parseInt(av)].length==1){
return [];
}
zz=getVal(getBox(av)+","+getRow(av)+","+getCol(av),true).split(",");
for(ii=0,len=zz.length;ii<len;ii++){
ss=arrFind(zz[ii],aa);
if(ss>-1){
aa.splice(ss,1);
}
}
return aa.join(",");
}
/*New Sudoku*/
function newSudoku(str){
var ii,len,ss;
window.perf=new Date().getTime();
window.log="";
window.sdk=[];
window.rem=[];
for(ii=0,len=str.length;ii<len;ii++){
ss=str.charAt(ii);
if(ss!="0"){
window.log+=ss+" on "+toRC(ii+"")+"<br>";
writeNumber(ss,ii+"");//wn
}else{
window.sdk[ii]=[];
window.rem.push(ii+"");
}
}
}
/*Write Number*/
function writeNumber(val,av,bol){
if(bol){
av=toAV(av);
}
window.sdk[parseInt(av)]=[val];
return delCand(val,av);
}
/*Delete Candidates*/
function delCand(val,av,bol){
var ii,jj,len,jen,ss,zz,cc,xx,xo,dd=[];
if(bol){
av=toAV(av);
}
zz=toRC(av);
xx=new RegExp(","+zz+",","g");//below bol
ss=toAV((getBox(av)+","+getRow(av)+","+getCol(av)).replace(xx,",").replace(zz+",","").replace(","+zz,"")).split(",");
for(ii=0,len=ss.length;ii<len;ii++){
cc=window.sdk[ss[ii]];
if(!cc||cc.length<2){
continue;
}
zz=arrFind(val,cc);
if(zz>-1){
window.sdk[parseInt(ss[ii])].splice(zz,1);
if(window.sdk[parseInt(ss[ii])].length==1){
window.log+=window.sdk[parseInt(ss[ii])][0]+" on "+toRC(ss[ii])+"<br>";
xo=delCand(window.sdk[parseInt(ss[ii])][0],ss[ii]);//wn
jen=xo.length;
if(jen>0){
for(jj=0;jj<jen;jj++){
dd.push(xo[jj]);
}
}
dd.push(ss[ii]);
}
}
}
return dd;
}
/*Delete Remaining*/
function delRem(arr){
var ii,len,ss;
for(ii=0,len=arr.length;ii<len;ii++){
ss=arrFind(arr[ii],window.rem);
if(ss>-1){
window.rem.splice(ss,1);
}
}
}
/*Solve Sudoku*/
function solSudoku(){
var ii,jj,ll,yy,len,jen,ss,zz,cc,dd,xo;
for(ll=0;ll<1;ll++){
dd=[];
for(ii=0,len=window.rem.length;ii<len;ii++){
zz=window.sdk[parseInt(window.rem[ii])];
if(zz.length==0){
ss=getCand(window.rem[ii]).split(",");
if(ss.length==1&&ss[0]!=""){
window.log+=ss[0]+" on "+toRC(window.rem[ii])+"<br>";
xo=writeNumber(ss[0],window.rem[ii]);//wn
jen=xo.length;
if(jen>0){
for(yy=0;yy<jen;yy++){
dd.push(xo[yy]);
}
}
dd.push(window.rem[ii]);
ll=-1;
}else{
if(ss.length>1){
window.sdk[parseInt(window.rem[ii])]=ss;
}
}
}else if(zz.length==1){
alert("solSudoku warning");
dd.push(window.rem[ii]);
}
}
delRem(dd);
}
if(window.rem.length!=0){
//failed to solve by simple solving
//start advanced solving
}
}
//============================================================================
newSudoku("032060007000008290000509000000300016800000002610002000000405000045800000700010530");
solSudoku();
//000124039007093056390000200040708000008050400000201060005000078270980300980517000
//032060007000008290000509000000300016800000002610002000000405000045800000700010530
//delCand("5","55",true);
//alert("["+sdk.join("][")+"]");
log+="--- Time: "+(new Date().getTime()-perf)+" milliseconds ---";
brd="<table id=\"brd\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr>";
for(i=0;i<sdk.length;i++){
if(i%9==0){
if(i!=0&&i!=80){
brd+="</tr><tr>";
}
}
if(sdk[i].length==0){
brd+="<td> </td>";
}
if(sdk[i].length==1){
brd+="<td>"+sdk[i]+"</td>";
}
if(sdk[i].length>1){
brd+="<td><span>"+sdk[i].join(" ")+"</span></td>";
}
}
brd+="</tr></tbody></table>";
log="<div id=\"log\">"+log+"</div>";
window.onload=function(){
document.body.innerHTML+=brd+log;
}
Share
Improve this question
asked Jan 26, 2012 at 4:53
mithril333221mithril333221
8192 gold badges10 silver badges20 bronze badges
2 Answers
Reset to default 8It works ok, see here
PS: You should select the no wrap (head)
option.
If you view the source of a jsFiddle you will see that all the javascript is already wrapped in the window.onload
function.
After the outer window.onload function is called, the inner function won't execute because the document is already loaded:
window.onload=function(){
window.onload=function(){
// this code will never get executed.
}
}