Hello Friends,
I am practicing on puzzle “There is no spoon- Episode 1”, till now i am doing well and fine.
but then i got a bug in nested for loop, but not able to point it out…
I hope someone just give me some advice tell me why this is happening?
I am attaching my code.
Any help will be appreciated. ThankYou!
Sorry for bad English!
See the bug Image Here…
var width = parseInt(readline()); // the number of cells on the X axis
var height = parseInt(readline()); // the number of cells on the Y axis
var xNodes = [];
var coords = {
xP: '0',
yP: '0',
xR: '-1',
yR: '-1',
xB: '-1',
yB: '-1'
};
for (var i = 0; i < height; i++) {
var line = readline(); // width characters, each either 0 or .
var nodeList = line.split('');
xNodes.push([]);
for(var j = 0; j < width; j++) {
if(nodeList[j] == '0') {
xNodes[i].push(nodeList[j]);
} else {
xNodes[i].push('-1');
}
}
}
for(var k = 0; k < xNodes.length; k++) {
for(var l = 0; l < width; l++){
switch(xNodes[k][l]) {
case '0' :
coords.xP = l;
coords.yP = k;
break;
case '.' :
coords.xP = '-1';
coords.yP = '-1';
break;
default:
coords;
break;
}
/// SEE this line is giving me some Hint!!
// but i am not able to point out!!
///>>>>
printErr("K: ", coords.xP + " L: ", coords.yP);
////<<<<
if((coords.xP+1 < width) && (xNodes[k][l+1] != '.')) {
coords.xR = l+1;
coords.yR = k;
} else {
coords.xR = '-1';
coords.yR = '-1';
}
if((coords.yP+1 < height) && (xNodes[k+1][l] != '.')) {
coords.xB = l;
coords.yB = k+1;
} else {
coords.xB = '-1';
coords.yB = '-1';
}
print(coords.xP+' '+coords.yP+' '+coords.xR+' '+coords.yR+' '+coords.xB+' '+coords.yB);
}
}