简体   繁体   English

使用JavaScript计算最近的礼物位置

[英]Calculate closest gift position using javascript

I tried one practice and check the requirement below. 我尝试了一种做法,并检查了以下要求。

You are on your way to find the gifts. 您正在寻找礼物的途中。 All the gifts lie in your path in a straight line at prime numbers and your house is at 0. Given your current position find the closest gift to your position, and calculate the distance between your current position and gift and tell the distance. 所有礼物都在直线上以质数直线位于您的路径上,并且房屋位于0。给定当前位置,找到与您的位置最近的礼物,并计算当前位置和礼物之间的距离并告诉距离。 Ex: For input 0, the output is 2 For number = 11, the output should be 0 For number = 2000000000, the output should be 11 For number = 1800000001, the output should be 10 例如:对于输入0,输出为2对于数字= 11,输出应为0对于数字= 2000000000,输出应为11对于数字= 1800000001,输出应为10

For the above logic I tried to use javascript and almost I have completed but i'm not getting the proper outpu as per the requirement,my output is returning any number. 对于上述逻辑,我尝试使用javascript并几乎已经完成,但是根据要求我没有得到正确的输出,我的输出返回任何数字。

Javascript Java脚本

function isPrime(num) {
    if (num <= 1) {
        return false;
    } else if (num <= 3) {
        return true
    } else if (num % 2 === 0 || num % 3 === 0) {
        return false
    }

    let i = 5
    while (i * i <= num) {
        if (num % i === 0 || num % (i + 2) === 0) {
            return false
        }
        i += 6
    }
    return true
}

HTML 的HTML

    <h1> Gift House</h1>
    <label for="name">Enter a house Number</label>
    <input type="text" id="inp" class="clr" />
    <input type="button" id="checker" value="Calculate" onClick="findpos()">
    <label for="name"> Distance of the gift house</label>
    <input type="text" id="demo" value="" class="clr">

I understood your requirement,you have tried 2 steps properly but before those two steps you have to do one more logic to calculate prime number.Because you have called the function isPrime in your JS but where you defined the function? 我了解您的要求,您已经尝试了2个步骤,但是在这两个步骤之前,您必须再做一次逻辑来计算素数。因为您在JS中调用了函数isPrime ,但是在哪里定义了该函数?

Just include the below script in your JS code and check the output. 只要在您的JS代码中包含以下脚本,然后检查输出即可。

function findpos() {
    var num = document.getElementById("inp").value;
    var pos = 0;
    while (true) {
        if (isPrime(num)) {
            break;
        } else {
            pos++;
            num++;
        }
    }
    document.getElementById("demo").value = pos;
}

Where is the isPrime function? isPrime函数在哪里? how you called without defining the function.I think this is what the reason for your issue. 您如何在不定义函数的情况下进行调用。我认为这就是您遇到问题的原因。

Check my below example, 检查我下面的例子,

function findpos() {
    var num = document.getElementById("inp").value;
    var pos = 0;
    while (true) {
        if (isPrime(num)) {
            break;
        } else {
            pos++;
            num++;
        }
    }
    document.getElementById("demo").value = pos;
}
function isPrime(num) {
    if (num <= 1) {
        return false;
    } else if (num <= 3) {
        return true
    } else if (num % 2 === 0 || num % 3 === 0) {
        return false
    }

    let i = 5
    while (i * i <= num) {
        if (num % i === 0 || num % (i + 2) === 0) {
            return false
        }
        i += 6
    }
    return true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM