More compact way to do this?
Posted
by Macha
on Stack Overflow
See other posts from Stack Overflow
or by Macha
Published on 2009-05-17T11:46:53Z
Indexed on
2010/05/22
15:00 UTC
Read the original article
Hit count: 156
I have a couple of functions that loop around the surrounding cells of a cell. The grid is contained inside an array.
In my code, I have checks to make sure it's not one of the edge cells, as checking an undefined cell causes an error.
As such, I have code like this:
if(x > 0) {
var firstX = x - 1;
} else {
var firstX = x;
}
if(x < 199) {
var lastX = x + 1;
} else {
var lastX = x;
}
if(y > 0) {
var firstY = y - 1;
} else {
var firstY = y;
}
if(y < 199) {
var lastY = y + 1;
} else {
var lastY = y;
}
A lot of lines of code to do very little. Is there a more elegant way to do this?
© Stack Overflow or respective owner