Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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>
    <dd class="result">!!!Answer!!!</dd>

    !!!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
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,335 Times in 4,301 Posts
    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
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,335 Times in 4,301 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,071
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    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>

  • Users who have thanked jmrker for this post:

    kmk123 (03-12-2013)

  • #5
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.

    Thanks for your reply anyway Old Pendant.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,335 Times in 4,301 Posts
    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.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,603
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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>"}
    
    document.getElementById("answer").innerHTML = ans;
    
    </script>

    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.

  • #9
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    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].
    Last edited by Airblader; 03-13-2013 at 09:12 PM.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Airblader View Post
    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!
    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.

  • #11
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    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
    •