Jump to content
Larry Ullman's Book Forums

Finding Maximum Of 3 Numbers


Recommended Posts

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 > B){

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...