0

I want to make a condition where the var UPS >= CURRENT along with the other conditions I set. i find the problems is when i compare a variable with another variable.

var BASE = document.getElementById("Base").value;
var UPS = document.getElementById("Ups").value;
var CURRENT = document.getElementById("Current").value;     
var calc = ((UPS-CURRENT)*10);
var result = parseInt(BASE)+parseInt(calc);


if(!isNaN(result) && BASE>0 && UPS>0 && CURRENT>=0 && UPS>=CURRENT)
{       
    document.getElementById("answer").innerHTML="Calculated Potential: "+result;
}
else
{
    document.getElementById("answer").innerHTML="Input valid numbers";
}

I want it so the calculations won't show if there is negative value and if the CURRENT value is more than the UPS value

DASDR
  • 13
  • 1

2 Answers2

0

You work with variables of string type. You should convert they to number before use in if statement. The simplest solution is:

if(!isNaN(result) && +BASE > 0 && +UPS > 0 && (+CURRENT >= 0 && +UPS >= +CURRENT)){       
    document.getElementById("answer").innerHTML="Calculated Potential: " +result;
} else {
    document.getElementById("answer").innerHTML="Input valid numbers";
}
Jackkobec
  • 5,889
  • 34
  • 34
0

Another very easy option:

const validInput = !isNaN(result) && +BASE > 0 && +UPS > 0 && (+CURRENT >= 0 && +UPS >= +CURRENT)
document.getElementById('answer').innerHTML = validInput ? `Calculated Potential: ${result}` : 'Input Valid Numbers'
Eunanibus
  • 112
  • 9