如何将八个标签的输入值添加到一个数组中,然后得到该数组的素数?Javascript

How to add the input values of eight labels to an array, then get the prime numbers of the array? Javascript

本文关键字:数组 然后 Javascript 添加 八个 标签 输入 一个      更新时间:2023-09-26

我在javascript中是非常新的,我想把8个标签(文本)的值放在8个数字的数组中,然后得到数组的素数,我可以制作数组,我可以在html中设置8个标签,但我不确定将这些值放在数组中。如果你能帮助我,那就太棒了,谢谢!

我的解算器按钮代码:

$('#btn1').click(function () {
    var primes = [true, true, true, true, true, true, true, true];
    var limit = Math.sqrt(8);
    for (var i = 1; i < 8; i++) {
        if (primes[i] === true) {
            for (var j = i * i; j < 8; j += i) {
                primes[j] = false;
            }
        }
    }
    for (var i = 1; i < 8; i++) {
        if (primes[i] === true) {
            console.log(i + " " + primes[i]);
        }
    }
});

标签代码:

<label>1. </label> <input id="input1" type="text"><br>
<label>2. </label> <input id="input2" type="text"><br>
<label>3. </label> <input id="input3" type="text"><br>
<label>4. </label> <input id="input4" type="text"><br>
<label>5. </label> <input id="input5" type="text"><br>
<label>6. </label> <input id="input6" type="text"><br>
<label>7. </label> <input id="input7" type="text"><br>
<label>8. </label> <input id="input8" type="text"><br>

检查number是否为素数的代码(来自:https://stackoverflow.com/a/24094774/1013526):

)
function isPrime(n) {
   // If n is less than 2 or not an integer then by definition cannot be prime.
   if (n < 2) {return false}
   if (n != Math.round(n)) {return false}
   // Now assume that n is prime, we will try to prove that it is not.
   var isPrime = true;
   // Now check every whole number from 2 to the square root of n. If any of these divides n exactly, n cannot be prime.
   for (var i = 2; i <= Math.sqrt(n); i++) {
      if (n % i == 0) {isPrime = false}
   }
   // Finally return whether n is prime or not.
   return isPrime;
}

代码求解器按钮点击:

$('#btn1').click(function () {
    var count = 8;   // in case you decide to change this later
    var primes = {}; // object instead of array
    for (var i = 1; i < count; i++) {
        var value = $('#input'+i).val(); // get the input value;
        value = parseInt(value); // convert it from string to int.
        primes[i] = {};
        primes[i].value = value;
        primes[i].isPrime = isNan(value) ? false : isPrime(value);
        $('#result'+i).text(primes[i].isPrime ? 'Prime' : 'Not Prime');
    }
    console.dir(primes); // log output to console.
    $('#container2').html(JSON.stringify(primes)); // output to container2 as string
});

标签代码(修改):

<label>1. </label> <input id="input1" type="text"><span id="result1"></span><br>
<label>2. </label> <input id="input2" type="text"><span id="result2"></span><br>
<label>3. </label> <input id="input3" type="text"><span id="result3"></span><br>
<label>4. </label> <input id="input4" type="text"><span id="result4"></span><br>
<label>5. </label> <input id="input5" type="text"><span id="result5"></span><br>
<label>6. </label> <input id="input6" type="text"><span id="result6"></span><br>
<label>7. </label> <input id="input7" type="text"><span id="result7"></span><br>
<label>8. </label> <input id="input8" type="text"><span id="result8"></span><br>

如果你想让输出更漂亮一点,你可以这样做:

var output = '<pre>';
for (var i in primes) {
    output += primes[i].value + ': ' + primes[i].isPrime + "'n";
}
output += '</pre>';
$('#container2').html(output);

您不需要创建数组并遍历数组中的每个元素。相反,您可以编写一个函数来检查传递的标签是否是素数。如果是素数,则填充HTML。我已经写了我的函数在PHP,但逻辑是相同的javascript。

function is_prime($number)
{
    if ($number==1)
        return false;
    if ($number==2)
        return true;
    $sqrt = sqrt($number);
    $floor = floor($sqrt);
    for ($i=2 ; $i <= $floor ; $i++)
    {
        if ($number % $i == 0)
        {
            return false;
        }
    }
    return true;
}
$start = 1;
$labels = 8;
for($i = 1; $i <= $labels; $i++)
{
    if(is_prime($i))
    {
        echo '<label>'.$i.'. </label>'.'<input id="input'. $i.'" type="text" value=" '. $i .'">'.'<br>';
    }
}
// output
2. 2
3. 3
5. 5
7. 7

这是我的整个HTML:

  <script>
        $(document).ready(function(){
            $('#btn1').click(function () {
                var count = 8;   // in case you decide to change this later
                var primes = {}; // object instead of array
                for (var i = 1; i < count; i++) {
                    var value = $('#input'+i).val(); // get the input value;
                    value = parseInt(value); // convert it from string to int.
                    primes.i.value = value;
                    primes.i.isPrime = isNan(value) ? false : isPrime(value);
                    $('#result'+i).text(primes.i.isPrime ? 'Prime' : 'Not Prime');
                    }
            $('#container2').html(primes); // log output to console.
            });
            function isPrime(n) {
            // If n is less than 2 or not an integer then by definition cannot be prime.
            if (n < 2) {return false}
            if (n != Math.round(n)) {return false}
            // Now assume that n is prime, we will try to prove that it is not.
            var isPrime = true;
            // Now check every whole number from 2 to the square root of n. If any of these divides n exactly, n cannot be prime.
            for (var i = 2; i <= Math.sqrt(n); i++) {
                if (n % i == 0) {isPrime = false}
            }
            // Finally return whether n is prime or not.
            return isPrime;
            }
        });
</script>      
</head>
<body>
    <center><h2>Prime Numbers Calculator.</h2><br></center>
    <div class="well">
        <div class="container" id="container1">
            <div class="col-md-4">
                <label>1. </label> <input id="input1" type="text"><span id="result1"></span><br>
                <label>2. </label> <input id="input2" type="text"><span id="result2"></span><br>
                <label>3. </label> <input id="input3" type="text"><span id="result3"></span><br>
                <label>4. </label> <input id="input4" type="text"><span id="result4"></span><br>
                <label>5. </label> <input id="input5" type="text"><span id="result5"></span><br>
                <label>6. </label> <input id="input6" type="text"><span id="result6"></span><br>
                <label>7. </label> <input id="input7" type="text"><span id="result7"></span><br>
                <label>8. </label> <input id="input8" type="text"><span id="result8"></span><br>
            </div>
        <div class="col-md-4">
            <div class="container" id="container2">
            </div>
        </div>
        <div class="col-md-4">
            <div class="container" id="container3">
            </div>
        </div>
        </div>
    </div>
        <center><button id="btn1" class="btn btn-primary"> Calculate</button>
        <button id="btn2" class="btn btn-primary"> Show</button>
        <button id="btn3" class="btn btn-primary"> Sort</button>
        <button id="btn4" class="btn btn-primary"> Clean</button></center>
</body>

但是仍然有一个问题显示结果在另一个容器

相关文章: