// 根据的数反求随机计算式 加减法
function getEquation(amount = 2, max = 20) {
res = Math.round(Math.random() * 20)
// 运算符
var operators = ['+', '-'];
var arr = [res];
for (var i = 1; i < amount; i++) {
// 获取随机运算符
var operator = operators[Math.round(Math.random() * 1)];
var a, b;
if (operator == '+') {
b = Math.round(Math.random() * arr[0]);
a = arr[0] - b;
} else {
b = Math.round(Math.random() * (max - arr[0]));
a = arr[0] + b;
}
arr.shift();
arr = [a, operator, b].concat(arr);
}
return {
eq: arr.join(' '),
res: res
}
}
for (var i = 1; i <= 50; i++) {
var equation = getEquation(Math.round(Math.random() * 1) + 2);
$('<p>' + equation.eq + ' = </p>').appendTo('body');
}
// console.log(JSON.stringify())
body{
padding: 3.5rem 1.5rem;
width: 210mm;
/* font-size: 1.75rem; */
column-count: 3;
background-color: #fff;
}
console