如果声明使我的程序在JavaScript中停止工作

时间:2016-06-22 12:03:27

标签: javascript if-statement

我的代码工作正常,直到我添加了if语句。非常感谢任何帮助。

function calc() {
   var a = parseInt(document.getElementById('eersteGetal').value); 
    var b = parseInt(document.getElementById('tweedeGetal').value); 
    var answer = parseInt(a + b);
    var quotient = a % b;
    var bigger = '';                                                
    if(a >= b) {                                
        bigger = "Number A is bigger";              
    } else if(a <= b) {                             
        bigger = "Number B is bigger";              
    } else(a === b) {                           
        bigger = "Number A en B are equal";     
    }
    var paragraaf = document.getElementById('answer'); 
    paragraaf.innerHTML = answer;
    var paragraafTwo = document.getElementById('quotient');  
    paragraafTwo.innerHTML = quotient;
    var paragraafThree = document.getElementById('bigger'); 
    paragraafThree.innerHTML = bigger;
}

3 个答案:

答案 0 :(得分:1)

else部分删除条件。

function calc() {
  var a = parseInt(document.getElementById('eersteGetal').value); 
  var b = parseInt(document.getElementById('tweedeGetal').value); 
  var answer = parseInt(a + b);
  var quotient = a % b;
  var bigger = '';                                                
  if(a > b) {                               
    bigger = "Number A is bigger";              
  } else if(a < b) {                          
    bigger = "Number B is bigger";              
  } else {
    bigger = "Number A en B are equal";     
  }
  var paragraaf = document.getElementById('answer'); 
  paragraaf.innerHTML = answer;
  var paragraafTwo = document.getElementById('quotient');  
  paragraafTwo.innerHTML = quotient;
  var paragraafThree = document.getElementById('bigger'); 
  paragraafThree.innerHTML = bigger;
}
<input type="text" id="eersteGetal">
<input type="text" id="tweedeGetal">
<button type="button" onclick="calc()">Calculate</button>
<p id="answer"></p>
<p id="quotient"></p>
<p id="bigger"></p>

答案 1 :(得分:0)

你不能在ELSE中提出一个条件。

将您的IF / ELSE-STATEMENT更改为:

if(a > b ) {                               
    bigger = "Number A is bigger";              
}
else if(a < b) {                          
    bigger = "Number B is bigger";              
}
else {
    bigger = "Number A en B are equal";     
}

答案 2 :(得分:0)

你错过了&#34;如果&#34;在最后的其他地方

else(a === b) {                            
bigger = "Number A en B are equal";     
}

应该是

else if(a === b) {                            
bigger = "Number A en B are equal";     
}