2015-02-12 3 views
0

У меня есть множитель, который я запрограммировал, который должен начинаться с проверки того, что значение, введенное в поле ввода, равно 1 или 0, поэтому он будет отображать «Коэффициент 1 равен 1.». или «Коэффициент 0 равен 0.», но когда я ввожу число 0 в поле ввода, он отображает «Коэффициент 0 is». но я не могу понять, почему. Вероятно, это простая ошибка, которую я не могу понять. Вот мой код. HTML:Функция не отображает корректные результаты

<!DOCTYPE HTML> 

<html lang="en"> 
    <head> 
     <title>JavaScript</title> 
     <script type="text/javascript" src="javaScript/prime.js"></script> 
    </head> 
    <body> 
     <!--Number--> 
     <h1>Factor finder</h1> 
     <p>Enter a number to find the factors!</p> 
     <form name="prime" id="here"> 
      Number: 
      <input class ="enter2" type="text" name="primeinput" size ="20"/> 
      <input type="button" name="addnumber" value="Go" onclick="findFactor();"/> 
      <br /> 
      <h2>Results</h2> 
     </form> 
    </body> 
</html> 

JavaScript:

//counters 
var numbernum = 0; 
var numinput = 1; 
//function 
function findFactor(){ 
    var array = new Array(); 
    //get input 
    thenumber=document.prime.primeinput.value; 
    //special circumstances for 1 and 0 
    if (thenumber == 1 || thenumber == 0){ 
     amount = "The factor of "; 
     verb = " is "; 
    } else{ 
     amount = "The factors of "; 
     verb = " are "; 
    } 
    //factor finder 
    for (i=1; i<thenumber + 1; i++){ 
     //check to see if the number is a factor 
     if(thenumber % i == 0){ 
      //check if the number is 1 or 0 to state the factors are 1 or 0 
      //not working with 0    
      if(thenumber == 1 || thenumber == 0){ 
       if (thenumber == 1){ 
        array[0] = 1; 
       } else if (thenumber == 0){ 
        array[0] = 0; 
       } 
      //if the number isn't 0 or 1 
      } else if(thenumber != 1 && thenumber != 0){ 
       if (thenumber == numinput){ 
        array[numbernum] = "and " + numinput; 
       } else{ 
        array[numbernum] = numinput; 
        numbernum ++ 
      } 

     } 

    } 
    numinput ++ 
} 
//append to the HTML 
var make = document.createElement("p"); 
var apply = document.createTextNode(amount + thenumber + verb + array.join(", ") + "."); 
make.appendChild(apply); 
document.getElementById("here").appendChild(make); 
//reset counters and clear array 
numbernum = 0; 
numinput = 1; 
var array = 0; 
} 
+0

Если 'thenumber' является '0', цикл завершается немедленно, так как' 1 Barmar

+1

Кстати, вам не нужно 'else if (thenumber! = 1 && thenumber! = 0)', просто используйте 'else'. Это просто противоположно условию 'if', а' else' выполняется только тогда, когда 'if' является ложным. – Barmar

ответ

0

В особых случаях 0 и 1, не заморачиваться с петлей for, так как вы знаете, что должно быть правильные результаты. Так положить if вне for:

if (thenumber < 2) { 
    array[0] = thenumber; 
} else { 
    for (var i = 1; i <= thenumber; i++) { 
     if (thenumber % i == 0) { 
      array.push(i); 
     } 
    } 
} 
0

для не петли из-за этого:

для (I = 1; я < + 1

Попробуйте изменить это на

для (i = 1; я < = Thenumber

Смежные вопросы