SOURCE

const questions = [
    {
        title: '2位(整)*3位(整)',
        algorithm: {
            three10Times: true,
            second10Times: true,
            maxResult: 10000,
        },
        func: 'threeDigitsTimesTwoDigits',
    },
    {
        title: '2位*3位(整)',
        algorithm: {
            three10Times: false,
            second10Times: true,
        },
    },
    {
        title: '比较',
        algorithm: {
            max: 10,
            operator: '-',
        },
    },
    {
        title: '10以内等式计算',
        algorithm: {
            maxResult: 100,
            operator: '-',
            int: 10,
        },
    },
    {
        title: '5内的乘法',
        algorithm: {
            max: 50,
            operator: '*',
            int: 10,
        },
    },
    {
        title: '20以内进位',
        algorithm: {
            max: 10,
            operator: '+',
        },
    },
    {
        title: '10为数连加',
        algorithm: {
            maxResult: 100,
            operator: '+',
            digit: 3,
            int: 10
        },
    },
    {
        title: '10为数连减',
        algorithm: {
            maxResult: 10,
            operator: '-',
            digit: 3,
            // int: 10
        },
    },
    {
        title: '加减混合',
        algorithm: {
            maxResult: 100,
            operator: '+-',
            digit: 3,
            int: 10
        },
    },
    {
        title: '退位计算',
        algorithm: {
            maxResult: 20,
            operator: '-',
            digit: 2,
        },
    },
]
// 三位数乘以两位数u
function threeDigitsTimesTwoDigits(question) {
    const {
        algorithm: { maxResult, three10Times, second10Times },
        questionNum = 10,
    } = question
    return new Array(questionNum).fill(null).map(() => {
        const threeDigits = three10Times ?
            (Math.floor(Math.random() * 50) + 10) * 10 :
            Math.floor(Math.random() * 500) + 100

        let maxSecondDigit = maxResult / threeDigits
        if (second10Times) {
            maxSecondDigit = maxSecondDigit / 10
        }
        maxSecondDigit = Math.floor(maxSecondDigit)
        // console.log("threeDigits",threeDigits)
        // console.log("maxSecondDigit",maxSecondDigit)
        const secondDigits = second10Times ?
            (Math.floor(Math.random() * maxSecondDigit) + 1) * 10 :
            Math.floor(Math.random() * (maxSecondDigit - 10) + 1) + 10
        const answer = threeDigits * secondDigits
        let expression = `${threeDigits} * ${secondDigits}`
        return {
            expression,
            answer: threeDigits * secondDigits
        }
    })
}

// 生成比较的题目
function generateCompare(question) {
    const {
        questionNum = 10,
        algorithm: { max, operator }
    } = question
    if (!operator) {
        return new Array(questionNum).fill(null).map(() => {
            let rn1 = Number.parseInt(Math.random() * max + 1)
            let rn2 = Number.parseInt(Math.random() * max + 1)
            let answer = ''
            if (rn1 < rn2) answer = '<'
            else if (rn1 > rn2) answer = '>'
            else if (rn1 === rn2) answer = '='
            return {
                rn1,
                rn2,
                answer
            }
        })
    }
    return new Array(questionNum).fill(null).map(() => {
        let rn1t = Number.parseInt(Math.random() * (max - 1) + 1)
        let rn2t = Number.parseInt(Math.random() * (max - rn1t) + 1)
        let rn3 = Number.parseInt(Math.random() * max + 1)
        const {
            answer,
            rn1,
            rn2
        } = generateQuestion(rn1t, rn2t, operator)
        let compare = ''
        if (answer < rn3) compare = '<'
        else if (answer > rn3) compare = '>'
        else if (answer === rn3) compare = '='
        return {
            rn1: `${rn1} ${operator} ${rn2}`,
            rn2: rn3,
            answer: compare,
        }
    })
}

// 进位等式
const generateCarryEquation = ({
    questionNum = 10,
    algorithm: {
        max, // 最大值
        operator,
    }
}) => {
    return new Array(questionNum).fill(null).map(() => {
        let num1 = Math.floor(Math.random() * (max - 1)) + 1
        let num2 = Math.floor(Math.random() * (num1 - 1)) + (max - num1)
        const { rn1, rn2, answer } = generateQuestion(num1, num2, operator)
        return {
            expression: `${rn1} + ${rn2}`,
            answer
        }
    })
}

// 退位等式
const generateAbdicateEquation=({
    questionNum = 10,
    algorithm: {
        maxResult, // 最大值
        operator,
    }
}) => {
    return new Array(questionNum).fill(null).map(() => {
        let num1 = Math.floor(Math.random() * (maxResult - 10 - 1)) + 10
        console.log(num1)
        let num2 = Math.floor(Math.random() * (num1 - 1)) + (max - num1)
        const { rn1, rn2, answer } = generateQuestion(num1, num2, operator)
        return {
            expression: `${rn1} + ${rn2}`,
            answer
        }
    })
}

// 生成等式计算题
const generateEquation = (question) => {
    let list = []
    const {
        questionNum = 10,
        algorithm: {
            max, // 单项最大取值
            maxResult, // 最大结果
            operator, // 运算符
            int = 1, // 倍数
            digit = 2, // 位数,默认两位数运算
        }
    } = question
    switch (operator) {
        case '+':
            list = new Array(questionNum).fill(0).map(() => {
                const max = maxResult / int
                let num1 = Math.floor(Math.random() * (max - 1)) + 1
                let num2 = Math.floor(Math.random() * (max - num1)) + 1
                let num1T = num1 * int
                let num2T = num2 * int
                let expression = `${num1T}+${num2T}`
                let answer = num1T + num2T
                // 3位数计算连加
                if (digit === 3) {
                    let num3 = (Math.floor(Math.random() * (max - num1 - num2)) + 0) * int
                    expression += `+${num3}`
                    answer += num3
                }
                return {
                    expression,
                    answer
                }
            })
            break;
        case '-':
            list = new Array(questionNum).fill(0).map(() => {
                const max = maxResult / int
                let num1 = Math.floor(Math.random() * (max - 1)) + 1
                let num2 = Math.floor(Math.random() * (max - num1)) + 1
                let num1T = num1 * int
                let num2T = num2 * int
                const { rn1, rn2 } = generateQuestion(num1T, num2T, operator)
                let expression = `${rn1}-${rn2}`
                let answer = rn1 - rn2
                // 3位数计算连减
                if (digit === 3) {
                    let num3 = (Math.floor(Math.random() * (rn1 - rn2)) + 0) * int
                    console.log(rn1, rn2, num3)
                    expression += `-${num3}`
                    answer -= num3
                }
                return {
                    expression,
                    answer
                }
            })
            break;
        case '+-':
            const addSubFlag = ['+', '-']
            return new Array(questionNum).fill(null).map(() => {
                const max = maxResult / int
                let num1 = Math.floor(Math.random() * (max - 1)) + 1
                let num2 = Math.floor(Math.random() * (max - num1)) + 1
                let num1T = num1 * int
                let num2T = num2 * int
                console.log(num1, num1T)
                console.log(num2, num2T)
                let opT = addSubFlag[Math.round(Math.random())]
                let { rn1, rn2, answer } = generateQuestion(num1T, num2T, opT)
                let expression = `${rn1}${opT}${rn2}`
                if (digit === 2) {
                    return {
                        expression,
                        answer,
                    }
                }
                const opT2 = addSubFlag[Math.round(Math.random())]
                let rn3 = 0
				if (opT2 === '+') {
					rn3 = (Math.floor(Math.random() * ((maxResult - answer)/int)) + 0)*int
                    answer += rn3
				} else if (opT2 === '-') {
					rn3 = (Math.floor(Math.random() * (opT === '+' ? answer/int :(rn1 - rn2)/int)) + 0)*int
                    answer -= rn3
				}
                
                expression+=`${opT2}${rn3}`
                return {
                    expression,
                    answer,
                }
            })
            break;
        case '*':
            list = new Array(questionNum).fill(0).map(() => {
                const maxT = max / int
                let num1 = Math.floor(Math.random() * (maxT - 1)) + 1
                let num2 = (Math.floor(Math.random() * (maxT - 1)) + 1) * int
                num1 = num1 * int
                const { rn1, rn2, answer } = generateQuestion(num1, num2, operator)
                return {
                    expression: `${rn1} * ${rn2}`,
                    answer
                }
            })
            break

    }
    return list
}

// 生成加减混合
const generateAddAndSubtract = ({
    questionNum = 10,
    algorithm: {
        max,
        maxResult, // 最大结果
        operator, // 运算符
        int = 1, // 倍数
    }
}) => {
    new Array(questionNum).fill(null).map(() => {

    })
}

const generateQuestion = (rn1, rn2, op) => {
    if (op === '+') {
        return {
            rn1,
            rn2,
            op,
            answer: rn1 + rn2
        }
    } else if (op === '-') {
        let [max, min] = [rn1, rn2]
        if (rn2 > max) {
            max = rn2
            min = rn1
        }
        return {
            rn1: max,
            rn2: min,
            op,
            answer: max - min
        }
    } else if (op === '*') {
        return {
            rn1,
            rn2,
            op,
            answer: rn1 * rn2
        }
    }
}
const funcs = {
    'threeDigitsTimesTwoDigits': threeDigitsTimesTwoDigits
}
const question = questions[questions.length - 1]
const list = generateAbdicateEquation(question)

console.log(list)
console 命令行工具 X clear

                    
>
console