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 5 of 5
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unchangable variable?

    Hi all.

    I used the function Math.max() to find the maximum number in a series of numbers. However, after it finds that number, I want it to say a specific thing if it is a certain number. Here's the code:

    Code:
    var h = Math.max(a,b,c,d,e,f,g)
    if (h==2){
    h=="Two"
    alert(h)
     }
    However, when the maximum does turn out to be 2, it alerts ("2") instead of ("Two"). I've also tried this:

    Code:
    var h = Math.max(a,b,c,d,e,f,g)
    var l = "string"
    if (h==2){
    l=="Two"
    alert(l)
     }
    But on the alert, it comes back as "string". What's wrong here and what can I do to make this work?

  • #2
    Regular Coder
    Join Date
    Apr 2009
    Posts
    244
    Thanks
    1
    Thanked 20 Times in 20 Posts
    Hey,

    Put
    l="Two"
    instead of
    l=="Two"

    Mike

  • Users who have thanked Mike_O for this post:

    CrackTheAir (06-02-2009)

  • #3
    New Coder
    Join Date
    May 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    might want to consider using a look-up table instead of forks in a situation like this:

    Code:
    var h = Math.max(a,b,c,d,e,f,g);
    var labels="Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten".split(",");
    
    alert(labels[h]);
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by rnd me View Post
    might want to consider using a look-up table instead of forks in a situation like this:

    Code:
    var h = Math.max(a,b,c,d,e,f,g);
    var labels="Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten".split(",");
    
    alert(labels[h]);
    That does not work! labels[h] is the maximum value of the list, (say 99), not the index of that value.

    Suggest:-

    Code:
    <script type = "text/javascript">
    
    var a = [5,26,4,96,7,24,45,38,52];
    var index;
    var max = a[0];
    var maxIndex = 0;
    for (index = 1; index < a.length; index++) {
    if (a[index] > max) {
    max = a[index];
    maxIndex = index;
    }
    }
    
    alert ("Maximum value is " + max + " at array index " + maxIndex);
    var labels="Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten".split(",");
    alert (labels[maxIndex]);
    
    </script>
    Last edited by Philip M; 06-03-2009 at 09:22 AM.


  •  

    Posting Permissions

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