-
Compare two numbers
almost 10 years ago
-
over 8 years ago
As far as I understand, when you get a value you get a string but then you try to compare 2 strings (not numbers). So, if you want to get more correct comparison you need to cast your strings to integer: a = Number(a);var a = document.getElementById("a").value
-
-
almost 10 years ago
Hi, You are passing value in displayResults function in a wrong way. Your code should be like this:
var a = document.getElementById("a").value; var b = document.getElementById("b").value; var compare = document.getElementById("comparison"); var submit = document.getElementById("submit"); submit.onClick = function() { WhoWins(a,b); } function WhoWins(a, b) { if(a === b){ displayResults ("="); }else if(a > b){ displayResults (">"); }else if(a < b){ displayResults ("<"); } function displayResults(result) { compare.innerHTML = result; }I hope it will give you expected result in field with compare id.
-

2 Answer(s)