简体   繁体   中英

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. 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

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

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

    <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?

Just include the below script in your JS code and check the output.

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? 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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