let result = 0;
const operators = ['+', '-'];
let question = '';
let lastOperator = '+'; // 初始操作符为加号
let lastNumber = null; // 初始化上一个数字为null
let buttonsDisabled = true; // 按钮初始状态为不可点击
for (let i = 0; i < bishu; i++) {//几笔
let number, operator;
let validNumber = false;
let result_onesDigit = onesDigit(result); //上一个数的个位
let result_tensDigit = tensDigit(result); //上一个数的十位
let result_hundredDigit = hundredDigit(result);//上一个数的百位
......省略
let result_yiDigit = yiDigit(result); //上一个数的亿位
// 循环直到找到一个合适的数字
do {
number = Math.floor(Math.random() * 999999999) + 1; // 随机生成百位数
let number_onesDigit=onesDigit(number);//个位
let number_tensDigit=tensDigit(number);//十位
let number_hundredDigit=hundredDigit(number);//百位
......省略
let number_yiDigit=yiDigit(number);//亿位
operator = operators[Math.floor(Math.random() * 2)];
if (operator === '+')
{
//百位
if((result_hundredDigit>4 && result_hundredDigit<9) || result_hundredDigit===0)
{
validNumber = result_hundredDigit + number_hundredDigit <= 9;
}
else if(result_hundredDigit===9)
{
validNumber = false;
}
else
{
validNumber = result_hundredDigit + number_hundredDigit <= 4;
}
}else{
//百位
if(result_hundredDigit===5)
{
validNumber = result_hundredDigit - number_hundredDigit == 0;
}
else if(result_hundredDigit===6)
{
validNumber = result_hundredDigit - number_hundredDigit == 0 || result_hundredDigit - number_hundredDigit == 1 || result_hundredDigit - number_hundredDigit == 5;
}
else if(result_hundredDigit===7)
{
validNumber = (result_hundredDigit - number_hundredDigit >= 0 && result_hundredDigit - number_hundredDigit <= 2) || result_hundredDigit - number_hundredDigit == 5 || result_hundredDigit - number_hundredDigit == 6;
}
else if(result_hundredDigit===8)
{
validNumber = (result_hundredDigit - number_hundredDigit >= 0 && result_hundredDigit - number_hundredDigit <= 3) || (result_hundredDigit - number_hundredDigit >= 5 && result_hundredDigit - number_hundredDigit <= 7);
}
else if(result_hundredDigit===9)
{
validNumber = result_hundredDigit - number_hundredDigit >= 0 && result_hundredDigit - number_hundredDigit <= 8;
}
else
{
validNumber = result_hundredDigit - number_hundredDigit >= 0;
}
}
if(validNumber)
{
if (operator === '+')
{
//十位
if((result_tensDigit>4 && result_tensDigit<9) || result_tensDigit===0)
{
validNumber = result_tensDigit + number_tensDigit <= 9;
}
else if(result_tensDigit===9)
{
validNumber = false;
}
else
{
validNumber = result_tensDigit + number_tensDigit <= 4;
}
}else{
//十位
if(result_tensDigit===5)
{
validNumber = result_tensDigit - number_tensDigit == 0;
}
else if(result_tensDigit===6)
{
validNumber = result_tensDigit - number_tensDigit == 0 || result_tensDigit - number_tensDigit == 1 || result_tensDigit - number_tensDigit == 5;
}
else if(result_tensDigit===7)
{
validNumber = (result_tensDigit - number_tensDigit >= 0 && result_tensDigit - number_tensDigit <= 2) || result_tensDigit - number_tensDigit == 5 || result_tensDigit - number_tensDigit == 6;
}
else if(result_tensDigit===8)
{
validNumber = (result_tensDigit - number_tensDigit >= 0 && result_tensDigit - number_tensDigit <= 3) || (result_tensDigit - number_tensDigit >= 5 && result_tensDigit - number_tensDigit <= 7);
}
else if(result_tensDigit===9)
{
validNumber = result_tensDigit - number_tensDigit >= 0 && result_tensDigit - number_tensDigit <= 8;
}
else
{
validNumber = result_tensDigit - number_tensDigit >= 0;
}
}
}
if(validNumber)
{
if (operator === '+')
{
//个位
if((result_onesDigit>4 && result_onesDigit<9) || result_onesDigit===0)
{
validNumber = result_onesDigit + number_onesDigit <= 9;
}
else if(result_onesDigit===9)
{
validNumber = false;
}
else
{
validNumber = result_onesDigit + number_onesDigit <= 4;
}
}
else
{
//个位
if(result_onesDigit===5)
{
validNumber = result_onesDigit - number_onesDigit == 0;
}
else if(result_onesDigit===6)
{
validNumber = result_onesDigit - number_onesDigit == 0 || result_onesDigit - number_onesDigit == 1 || result_onesDigit - number_onesDigit == 5;
}
else if(result_onesDigit===7)
{
validNumber = (result_onesDigit - number_onesDigit >= 0 && result_onesDigit - number_onesDigit <= 2) || result_onesDigit - number_onesDigit == 5 || result_onesDigit - number_onesDigit == 6;
}
else if(result_onesDigit===8)
{
validNumber = (result_onesDigit - number_onesDigit >= 0 && result_onesDigit - number_onesDigit <= 3) || (result_onesDigit - number_onesDigit >= 5 && result_onesDigit - number_onesDigit <= 7);
}
else if(result_onesDigit===9)
{
validNumber = result_onesDigit - number_onesDigit >= 0 && result_onesDigit - number_onesDigit <= 8;
}
else
{
validNumber = result_onesDigit - number_onesDigit >= 0;
}
}
}
......省略
if(validNumber)
{
if (operator === '+')
{
//亿位
if((result_yiDigit>4 && result_yiDigit<9) || result_yiDigit===0)
{
validNumber = result_yiDigit + number_yiDigit <= 9;
}
else if(result_yiDigit===9)
{
validNumber = false;
}
else
{
validNumber = result_yiDigit + number_yiDigit <= 4;
}
}
else
{
//亿位
if(result_yiDigit===5)
{
validNumber = result_yiDigit - number_yiDigit == 0;
}
else if(result_yiDigit===6)
{
validNumber = result_yiDigit - number_yiDigit == 0 || result_yiDigit - number_yiDigit == 1 || result_yiDigit - number_yiDigit == 5;
}
else if(result_yiDigit===7)
{
validNumber = (result_yiDigit - number_yiDigit >= 0 && result_yiDigit - number_yiDigit <= 2) || result_yiDigit - number_yiDigit == 5 || result_yiDigit - number_yiDigit == 6;
}
else if(result_yiDigit===8)
{
validNumber = (result_yiDigit - number_yiDigit >= 0 && result_yiDigit - number_yiDigit <= 3) || (result_yiDigit - number_yiDigit >= 5 && result_yiDigit - number_yiDigit <= 7);
}
else if(result_yiDigit===9)
{
validNumber = result_yiDigit - number_yiDigit >= 0 && result_yiDigit - number_yiDigit <= 8;
}
else
{
validNumber = result_yiDigit - number_yiDigit >= 0;
}
}
}
上面的是9位数的每一位上的数字计算的一些判断,怎么简化一下?麻烦写详细一点