js输入一个数,判断是质数还是合数

    技术2022-07-10  76

    <input type="number" id="x"> <button id="btn">查询</button> <input type="text" id="y"> <script> btn.onclick = function () { var xx = x.value - 0;//获取数字输入框的值 var flag = 1; for (var i = 2; i < xx; i++) { if (xx % i == 0) { flag = 2; break; } } flag == 1 ? y.value = '质数' : y.value = '合数';//判断是质数还是合数,输出到文本输入框 } </script>
    Processed: 0.019, SQL: 9