I tried to make a word search solver. So the purpose of this tool is to count how many times the word given appear in the grid a.k.a array in this project. But I got stuck when I made a code to check vertically, It said "TypeError: Cannot read property '3' of undefined"
Here is the code
var baris = [3];
var kolom = [8];
var kata = ["zpw"];
var puzzle= [
["z","x","x","r","r","r","f","z"],
["p","l","q","z","h","h","r","p"],
["w","o","l","p","p","o","o","w"]
];
var i;
var k;
var l;
var x;
var y;
var r=0;//left-right
var s=0;//right-left
var t=0;//up-down
var u=0;//down-up
var z=0;
var a=0;
var n=0;
var p=0;
function vertical() {
for (i=0;i<baris.length;i++) {
for(k=p;k<(baris[i]+baris[i-1] ||baris[i]);k++ ) {
for(l=0; l<kolom[i];l++) {
if(puzzle[k][l] == kata[i].charAt(0)) { //up-down CHECKER
for(y=1;y<kata[i].length ;y++) {
if(kata[i].charAt(y) == puzzle[k+y][l]) {
n=n+1;
console.log(n);
}
}
if(n==kata[i].length-1) {
t=t+1;
}
n=0;
}
}
//console.log(t);
}
t=0;
u=0;
p=p+baris[i];
}
}
vertical();`
Hope you guys can help to find the problem. Thank you very much.
I guess what do you need it is make a "clean" code, just think about algorithm, write in on paper, draw it on paper, think again, then create prototype, then refactor, so you will have algorithm divided into autonomous isolated functions. As a reward you will get more-less understanding code, which will be flexible, real to debug, ready to improve and extend.
As I see, you will need something that will:
Here is hasty written code, without any "fool protection", will work only on "puzzles" with same rows' sizes, and might be pretty slow: