Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: if else statements multiple conditions

1. ## if else statements multiple conditions

Hi I'm new here and new to javascripting too. I have picked up other languages quite quickly, but javascript is proving more difficulf for me.

I need javascript to return max number of three numbers using if else statements with multiple conditions.

So far what i have is not working and I'm stuck. My appologies for the clumsy coding. Any help would be greatly appreciated.

<body>
<p>Define a function maxOfThree() that takes three numbers as arguments and returns the largest of them.<br/>
If 2 values are the highest it should say which 2. If all values are the same it should let the user know</p>
<dl>
<dt>84, 35, 45</dt>
<script type="text/javascript" language="javascript">
var a = 84;
var b = 35;
var c = 45;
var d = maxOfThree();

function(maxOfThree)
{if (a == b)&&(a == c){
return "all values are equal";

{else if(a == b)&&(a > c){
return "Values 1 & 2 are greater";

}else if(a == b)&&(a < c){
return c;

}else if(a == c)&&(a > b){
return "values 1 & 3 are greater";

}else if(a == c)&&(a < b){
return b;

}else if(b == c)&&(b > a){
return "values 2 & 3 are greater";

}else if(b == c)&&(b < a){
return a;

}

</script>

!!!Answer!!! is where i need the result to return to but I think my fuction is flawed, my logic behind it may also be flawed and I haven't yet contemplated how or where to get the answer to return where I need it to.

2. What other languages have you "picked up"?

Surely at least one of those other languages used functions.

And surely those functions could take multiple arguments.

Write the solution to this problem in one of those other languages you say you know and then we can show you how JavaScript compares pretty much one for one with them.

You do need to also learn to manipulate the browser Document Object Model ("DOM"), but until you can write a function taking multiple arguments, you aren't ready to do that.

If you have tumbled to what I have been saying, yet, then understand that you haven't even got the first line of your function correct. Read again the requirement:
Define a function maxOfThree() that takes three numbers as arguments

3. I will tell you that, as written, your HTML won't work with the JS code.

A good start would be to pull your entire <dl> ahead of the <script> tag. And make it easy on your self by giving your <dd> tag an id.
Code:
```<dl>
<dt>84, 35, 45</dt>
<dd id="result"></dd>
</dl>
<script type="text/javascript">
...
</script>```
Oh...and `language="javascript"` is way obsolete. Kill it.

4. Can you modify this ...
Code:
```<script type="text/javascript" language="javascript">
var a = 84;
var b = 35;  // change to 84 for testing
var c = 45;  // change to 84 for testing
var d = maxOfThree(a,b,c);
alert('Max value of '+a+', '+b+', or '+c+' is '+d);

function maxOfThree(x,y,z) {
var max;
if (x > y) { max = +x;} else { max = +y; }
if (max < z) { max = +z; }
var cnt = 0;
if (max == x) { cnt++; }
if (max == y) { cnt++; }
if (max == z) { cnt++; }
if (cnt == 3) { alert(x+', '+y+' are '+z+' are equal')
} else {
if (cnt == 2) { alert('two values are equal'); }
}
return max;
}
</script>```

5. ## Users who have thanked jmrker for this post:

kmk123 (03-12-2013)

6. ## reply to old pendant

I have picked up php and asp.net relatively easy however I am not allowed to use them in this problem.

As i stated, I am a complete newcomer to javascript and was hoping someone would be helpful in pointing me in the right direction. After all, we were all beginners at some point huh? I have had 2 classes in javascripting so far so I am at a very basic entry level.

Thanks for your reply anyway Old Pendant.

7. So in PHP, how would you write a function "that takes three numbers as arguments"??

Surely you would write
Code:
```function maxOfThree( \$a, \$b, \$c )
{
...
}```
No? So why didn't you do essentially the same thing in JavaScript? [In fact, you could have written it exactly like that. JavaScript doesn't require a \$ as the first character of user-defined variables, but it does allow it.]

And the if ( ) statement in PHP is essentially identical to if ( ) in JavaScript, yet you didn't use legal if's in your code.

I did *NOT* say you should write the assignment in PHP (or ASP.NET or ... ). I said show how you would have coded it in one of those languages (PHP would be the closest to JavaScript). And *THEN* we could show you what changes you needed to convert it to JavaScript.

Look here: http://www.codingforums.com/rules.htm
And look especially at rule 1.5

JMrkr really should not have given you the answer as he did. How will you learn if you get the answers from us? How will you perform when it comes time to take a test and we aren't there to help you? And what will you do if your instructor is clever enough to come visit forums such as this and find out that you are getting the answers without doing the work? (Were I your instructor and found this out, you'd be in severe danger of failing the class.)

8. You can find out which of the values is greatest using a single command.

`Math.max(a, b, c);`

You only need the if statements to test which of the three are equal to that maximum.

9. Code:
```<div id = "answer"></div>

<script type = "text/javascript">

var a = 10;
var b = 30;
var c = 30;
var highest = Math.max(a, b, c);
var ans = "Highest value(s):- <br> ";
if (a==highest) {ans += "a value " + a + " is highest <br>"}
if (b==highest) {ans += "b value " + b + " is highest <br>"}
if (c==highest) {ans += "c value " + c + " is highest <br>"}

</script>```

10. For once I'll post dirt, ugly code – which will save you the if/else statements:

Code:
```var max = Math.max(a,b,c);
var cnt = 1*(a==max)+1*(b==max)+1*(c==max);```
But keep in mind that readability is considered important these days, especially if you ever consider doing this as a job. So you might be better of using something readable and extensible, i.e. using a forEach function on [a, b, c].

11. Originally Posted by Airblader
For once I'll post dirt, ugly code – which will save you the if/else statements:

Code:
```var max = Math.max(a,b,c);
var cnt = 1*(a==max)+1*(b==max)+1*(c==max);```
But keep in mind that readability is considered important these days, especially if you ever consider doing this as a job. So you might be better of using something readable and extensible, i.e. using a forEach function on [a, b, c].
That says how many variables have (share) the hightest value, but not which ones they are. It is not much use to be told that one of the numbers (unspecified) is the highest. We sort of knew that!

12. I was going by the snippet jmrker posted – I guess it really doesn't fit what the OP posted. And yes, cnt >= 1 is trivial.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•