Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
sonal

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;

Share this post


Link to post
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 this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

Loading...
Sign in to follow this  

×
×
  • Create New...