sonal Posted July 24, 2012 Share Posted July 24, 2012 What is wrong with following neseted if conditions? This program shows wrong result to compare 3 integer numbers and find out maximum number. I'm so confused. This program gives a wrong maximum number. var a = prompt("Enter first number"); var b = prompt("Enter second number"); var c = prompt("Enter third number"); var answer; document.getElementById("no1").innerHTML = "a : " + a; document.getElementById("no2").innerHTML = "b : " + b; document.getElementById("no3").innerHTML = "c : " + c; if(a > { if (a > c) { //alert("a is maximu : " + a); answer = a +" a is maximum"; } else { //alert("c1 is max : " + c); answer = c + " c1 is maximum"; } } else { if(b > c) { answer = b +" b is maximum"; } else { //alert("c2 is maximum : " + c); answer = c + " c2 is maximum"; } } document.getElementById("show").innerHTML = answer; Link to comment Share on other sites More sharing options...
sonal Posted July 24, 2012 Author Share Posted July 24, 2012 I got it corrected. a, b and c values were coming from text type form fields. So they were comparing as strings rather than numbers. I had to use parseInt function to convert them to numbers before comparing them. That is a simple example to show how to use nested if. Thanks for looking.... Link to comment Share on other sites More sharing options...
Larry Posted July 25, 2012 Share Posted July 25, 2012 Kudos for getting it working and thanks for sharing the solution. 1 Link to comment Share on other sites More sharing options...
Recommended Posts