javascript "less than" if statement failing

if-statement, javascript

Solution

When you pull the `.value` of an element it returns a string. `'10'<'2'` will return true.

You can simply do a parseInt/parseFloat on the value, ala

var q = parseInt(document.getElementById("Q"+i).value,10)

Problem

Here is my function: ``` function reCalculate(i) { document.getElementById("Q" + i).value = document.getElementById("C" + i).value - document.getElementById("QA" + i).value; if (document.getElementById("Q" + i).value < 0) { document.getElementById("Q" + i).value = 0; } if (document.getElementById("Q" + i).value < document.getElementById("E" + i).value && document.getElementById("Q" + i).value != 0) { alert(document.getElementById("Q" + i).value + " is less than " + document.getElementById("E" + i).value + "?"); document.getElementById("Q" + i).value = document.getElementById("E" + i).value; } document.getElementById("Q" + i).value = Math.ceil(document.getElementById("Q" + i).value); } ``` It checks Q, if it's less than 0, it makes it 0. Then, if it's not 0, but it's less than E, it makes it E. For some reason this function works UNLESS Q is a double digit number. For example, if Q is 7 and E is 2, then it will leave Q at 7. However, if Q is 10 and E is 2, for some reason it thinks that 10<2, and it changes Q to 2! Am I missing something here??

Original source