Christiaan Erkelens Christiaan Erkelens - 5 months ago 9
Javascript Question

if statement makes my program in javascript stop working

My code was working fine till I added the if statement. Any help is much appreciated.

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

Answer

You cant put a condition in an ELSE.

Change your IF/ELSE-STATEMENT to this:

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";     
}
Comments