有没有办法简化我的代码部分?我一直在研究这个问题,最后得到了这个,或者做了我需要做的事情。我有一个数组(6 * 6),当你点击图像时,它会转动所有其他图像相同的颜色。我只使用红色和蓝色,对不起我没有使用分号,但我稍后会修复它。那么有人可以帮我吗?如果您需要整个程序,我也可以上传。
function vClick(iRow, iCol)
{
var i, j;
if (astrColor[iRow][iCol] == 'r')
{
if ((iRow - 1) < 0)
{
for (i = iRow; i <= (iRow + 1) ; i++)
{
if ((iCol - 1) < 0)
{
for (j = iCol; j <= (iCol + 1) ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'rcircle.png';
astrColor[i][j] = 'r';
}
}
else if ((iCol + 1) > 5)
{
for (j = (iCol - 1) ; j <= iCol; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'rcircle.png';
astrColor[i][j] = 'r';
}
}
else
{
for (j = (iCol - 1) ; j <= (iCol + 1) ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'rcircle.png';
astrColor[i][j] = 'r';
}
}
}
}
else if ((iRow + 1) > 5)
{
for (i = (iRow - 1) ; i <= iRow; i++)
{
if ((iCol - 1) < 0)
{
for (j = iCol; j <= (iCol + 1) ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'rcircle.png';
astrColor[i][j] = 'r';
}
}
else if ((iCol + 1) > 5)
{
for (j = (iCol - 1) ; j <= iCol; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'rcircle.png';
astrColor[i][j] = 'r';
}
}
else
{
for (j = (iCol - 1) ; j <= (iCol + 1) ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'rcircle.png';
astrColor[i][j] = 'r';
}
}
}
}
else if((iCol - 1) < 0)
{
for (i = (iRow - 1) ; i <= (iRow + 1) ; i++)
{
for (j = iCol ; j <= (iCol + 1) ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'rcircle.png';
astrColor[i][j] = 'r';
}
}
}
else if((iCol + 1) > 5)
{
for (i = (iRow - 1) ; i <= (iRow + 1) ; i++)
{
for (j = (iCol - 1) ; j <= iCol ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'rcircle.png';
astrColor[i][j] = 'r';
}
}
}
else
{
for (i = (iRow - 1) ; i <= (iRow + 1) ; i++)
{
for (j = (iCol - 1) ; j <= (iCol + 1) ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'rcircle.png';
astrColor[i][j] = 'r';
}
}
}
}
else
{
if ((iRow - 1) < 0)
{
for (i = iRow; i <= (iRow + 1) ; i++)
{
if ((iCol - 1) < 0)
{
for (j = iCol; j <= (iCol + 1) ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'bcircle.png';
astrColor[i][j] = 'b';
}
}
else if ((iCol + 1) > 5)
{
for (j = (iCol - 1) ; j <= iCol; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'bcircle.png';
astrColor[i][j] = 'b';
}
}
else
{
for (j = (iCol - 1) ; j <= (iCol + 1) ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'bcircle.png';
astrColor[i][j] = 'b';
}
}
}
}
else if ((iRow + 1) > 5)
{
for (i = (iRow - 1) ; i <= iRow; i++)
{
if ((iCol - 1) < 0)
{
for (j = iCol; j <= (iCol + 1) ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'bcircle.png';
astrColor[i][j] = 'b';
}
}
else if ((iCol + 1) > 5)
{
for (j = (iCol - 1) ; j <= iCol; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'bcircle.png';
astrColor[i][j] = 'b';
}
}
else
{
for (j = (iCol - 1) ; j <= (iCol + 1) ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'bcircle.png';
astrColor[i][j] = 'b';
}
}
}
}
else if ((iCol - 1) < 0)
{
for (i = (iRow - 1) ; i <= (iRow + 1) ; i++)
{
for (j = iCol ; j <= (iCol + 1) ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'bcircle.png';
astrColor[i][j] = 'b';
}
}
}
else if ((iCol + 1) > 5)
{
for (i = (iRow - 1) ; i <= (iRow + 1) ; i++)
{
for (j = (iCol - 1) ; j <= iCol ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'bcircle.png';
astrColor[i][j] = 'b';
}
}
}
else
{
for (i = (iRow - 1) ; i <= (iRow + 1) ; i++)
{
for (j = (iCol - 1) ; j <= (iCol + 1) ; j++)
{
var strID = "img" + i + "," + j;
document.getElementById(strID).src = 'bcircle.png';
astrColor[i][j] = 'b';
}
}
}
}
}
答案 0 :(得分:0)
DRY将相同的代码块放在辅助方法中,然后调用它们。
它还有助于整个代码的可读性,就像现在一样,我可以告诉你,对于列和行是否在某个值范围内,你正在做一堆逻辑。
为了获得更好的建议,请用简单的英语解释你的算法(思维方式),因为我可以老老实实地读什么。
答案 1 :(得分:0)
我假设您需要更改周围的所有图像,而不仅仅是上,下,左,右。此外,我假设您将正确调用vClick(我的意思是仅在需要时)。最后,这项工作可能无法完美运作,因为这是第一个想法。
我的想法是只迭代一次,从[i-1,j-1]到[i + 1,j + 1],并使用单个函数,只有当它存在时才会更改给定项目。
function vClick(iRow, iCol)
{
for (i = iRow-1; i<=iRow+1; i++)
for (j = iCol-1; j<=iCol+1; j++)
changeImage(i,j);
}
function changeImage(i,j)
{
var strID = "img" + i + "," + j;
var theImage = document.getElementById(strID);
if (theImage != null)
if (theImage.src == 'bcircle.png')
{theImage.src = 'rcircle.png'; astrColor[i][j] = 'r';
else
{theImage.src = 'bcircle.png'; astrColor[i][j] = 'b';}
}