技术笔记
当前位置:首页 > 商学院 > 技术笔记 > 正文内容

技术笔记

js生成100以内的退位减法练习题

豫唐网络2023-10-25 18:421398

用JavaScript生成10道100以内的退位减法练习题

退位减法是指从一个数中减去另一个数,如果被减数的个位数小于减数的个位数,就需要从十位数中借位。

先放效果图

34.jpg

代码如下

function generateBorrowSubtractionProblem() {
    let minuend;
    let subtrahend;
    do {
        minuend = getRandomNumber(10, 99);
        subtrahend = getRandomNumber(10, minuend);
    } while (minuend % 10 >= subtrahend % 10);
   
    const problem = `${minuend} - ${subtrahend} = ?`;
    const answer = minuend - subtrahend;
   
    return { problem, answer };
}

function getRandomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

// 生成10个问题
for(let i = 0; i < 1000; i++) {
    const { problem, answer } = generateBorrowSubtractionProblem();
    console.log(`${i+1}. ${problem}`);
    console.log(`答案: ${answer}\n`);
}

这段代码定义了两个函数:generateBorrowSubtractionProblem 和 getRandomNumber。generateBorrowSubtractionProblem 函数用于生成退位减法问题,它首先随机生成一个被减数和一个减数,然后判断被减数的个位数是否大于等于减数的个位数,如果是,则重新生成被减数和减数,直到被减数的个位数小于减数的个位数。然后,它将被减数、减数和答案作为一个对象返回。

getRandomNumber 函数用于生成一个指定范围内的随机整数。

扫描二维码推送至手机访问。

版权声明:本文由汤阴县豫唐网络科技有限公司发布,如需转载请注明出处。

本文链接:https://www.ytecn.com/post/892.html

分享给朋友:

相关文章

vscode运行php和Composer

vscode运行php和Composer

需要用到的工具1、安装php(官网下载)2、安装composer(官网)3、vscode插件PHP Server4、vscode插件PHP Debug5、windows 11系统步骤1、安装php安装...

phpRSA加密解密函数

phpRSA加密解密函数

使用方法:加密 $txt="ytecn";  $pubkey="公钥"  $macdata = RSAEn...

PHP获取文件后缀名

PHP获取文件后缀名

获取文件后缀名,识别当前文件是什么类型的。应用案例$url="1.txt"; $name=GetFileExt($url); print_r($name);输出结果为:txt函...

php清除一串代码内所有的PHP代码

php清除一串代码内所有的PHP代码

保存数据的时候,为了安全考虑,会用到这类功能,清除一串代码内所有的PHP代码。应用案例$url="1asasdasdas<?php echo 'ssss&#...

weburl跳转加好友

weburl跳转加好友

目前大多数主题、程序、插件仍然使用着多年前的QQ客服私聊、加好友接口,但这个接口官方已把网页关闭。这里找了个ntqq的加好友接口,用户唤起ntqq桌面端、手机端资料卡tencent://ntqq-op...