经典JS Prime编号

时间:2017-02-04 17:59:11

标签: javascript primes

这可能是一个愚蠢的帖子,但我无法弄清楚为什么我的代码无法正常工作。为了记录,我已经编写了不同的代码,但我仍然不明白为什么这个代码没有。

为什么第二个if语句总是传递为true?

https://jsfiddle.net/vhdh5x3d/2/

/* This code doesn't work... */
var testNumber = prompt("Please enter a number you'd like to test");

for (var i = 2; i < testNumber; i++) {
    if (testNumber % i == 0) {
    document.getElementById("test-results").innerHTML = "Your number, " + testNumber + ", is not a prime number.";
  }
  if ((testNumber - 1) == i && testNumber % i != 0 ) {
    document.getElementById("test-results").innerHTML = "Congratulations! Your number, " + testNumber + ", is a prime number!";
  }
}

5 个答案:

答案 0 :(得分:0)

您的代码似乎假设,给定一个正整数testNumber和另一个正整数i,那么如果((testNumber - 1) == i) && ((testNumber % i) != 0)表示testNumber是素数。

这个假设是错误的。例如,考虑案例testNumber = 8i = 7。上述条件成立,但8不是素数。

答案 1 :(得分:0)

第二个if并不总是正确的,它只在for循环的最后一次迭代中,所以你在html中看到的是找到的主要信息。

你应该这样做:

var testNumber = prompt("Please enter a number you'd like to test");
var prime = true;
for (var i = 2; i < testNumber; i++) {
    if (testNumber % i == 0) {
      prime = false;

  }
}

if (prime == true) 
  document.getElementById("test-results").innerHTML = "Congratulations! Your number, " + testNumber + ", is a prime number!";
else
  document.getElementById("test-results").innerHTML = "Your number, " + testNumber + ", is not a prime number.";

这是一个简单的算法,可以测试数字是否为素数。您可以为此找到许多不同的算法。

答案 2 :(得分:0)

我将解决您的代码中的一些问题。

  1. 从不使用.innerHTML使用此方法存在很大的安全风险。永远不要直接从用户注入任何评估代码的东西。同样适用于.eval()。请考虑设置.textContent

  2. 考虑...

    const testNumber = prompt("Please enter a number you'd like to test");
    if (testNumber === 1) {
       document.getElementById("test-results").textContent = 'Sorry, this is not a prime number.'
    } else if (testNumber === 2) {
      document.getElementById("test-results").textContent = 'Congratulations, you entered a prime number.'
    } else {
    for (var x = 2; x < testNumber; x++) {
      if (testNumber % x === 0) {
         document.getElementById("test-results").textContent = 'Sorry, this is not a prime number.'
      }
    }
      document.getElementById("test-results").textContent = 'Congratulations, you entered a prime number.'
    }
    

答案 3 :(得分:0)

第二个If语句总是不正确。它将仅在最后一次迭代期间进入第二个if语句并设置html。 一旦满足您的条件,您将始终必须返回该值,而您的代码中没有这样做。

下面的代码可以使用:

var testNumber = prompt("Please enter a number you'd like to test");

for (var i = 2; i < testNumber; i++) {
    if (testNumber % i == 0) {
    document.getElementById("test-results").innerHTML = "Your number, " + testNumber + ", is not a prime number.";
return;
  }
  if ((testNumber - 1) == i && testNumber % i != 0 ) {
    document.getElementById("test-results").innerHTML = "Congratulations! Your number, " + testNumber + ", is a prime number!";
return;
  }
}

答案 4 :(得分:-1)

var testNumber = prompt("Please enter a number you'd like to test");
for (var i = 2; i < testNumber; i++) {
  if (testNumber % i == 0) {      
     break;//already found a working number, so stop
  }
}
//if last iteration reached without breaking its a prime
document.getElementById("test-results").innerHTML = "Your number, " + testNumber + ", is "+(testNumber==i?"a":"not a")+" prime number.";

http://jsbin.com/qabivasiyu/edit?console

正如评论已经指出的那样,第二个if不应该在for循环中,第一个if应该打破循环。我也简化了你的代码......