Search code examples
javascriptfunctionif-statementbuttonassign

Button assigned with function wont do as i tell it too


Im trying to create a script. The goal is that the user will put in 3 numbers in 3 boxes, and click a button to display the highest number inside a forth box displayed in the bottom.

For some reason the code i have created does not do what i have written and i do not understand why. Any hints of what the problem is so i can dig into it myself is appreciated!

Here is a live veiw in browser on how it looks so you can see what happens when i press the button.

https://gyazo.com/1f825e84930891f8727aed6032b94edd

function checkNumbers(){

    //(Check all the numbers the user put in)
    var box1 = document.getElementById("userFill1").value*1;
    var box2 = document.getElementById("userFill2").value*1;
    var box3 = document.getElementById("userFill3").value*1;

//(If box1 > b2 & b3 write box1 value in "answer")
if(box1 > box2 && box3){
document.getElementById("answer").value = box1;
}

//(Same as above but with box 2)
else if(box2 > box1 && box3){
    document.getElementById("answer").value = box2;
}

//(Same as above but with box 3)
else if(box3 > box1 && box2){
    document.getElementById("answer").value = box3;
}

//(If no statements above is true, alert this)
else{
    alert("Vänligen fyll i med endast siffror och olika värden i varje fält");
}

}

Solution

  • Just use document.getElementById("answer").value = Math.max(box1,box2,box3) instead of all this ifology