If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

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

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

Users who have thanked jmrker for this post:

reply to old pendant

Thanks for your reply,

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.

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.

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

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

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!

Last edited by Philip M; 03-13-2013 at 09:55 PM.

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.