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");

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";

}

}

##### 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....

##### Share on other sites

Kudos for getting it working and thanks for sharing the solution.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.