随机生成加减法
const problems = [];
for (let i = 0; i < 10; i++) {
const num1 = Math.floor(Math.random() * 10) + 1;
const num2 = Math.floor(Math.random() * 10) + 1;
const operator = Math.random() < 0.5 ? '+' : '-';
const result = operator === '+' ? num1 + num2 : num1 - num2;
if (result > 0 && result < 10) {
problems.push(`${
num1} ${
operator} ${
num2} = ${
result}`);
} else {
i--;
}
}
console.log(problems);