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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question output two values of an array

    Hi
    How can i output two maximum numbers of an array if they are equal?

    var flower["roses", "violets", "buttercups", "daisies"];
    var flowerAmounts[9, 8, 3, 9];

    Output should be:
    The maximum amount of flowers is 9.
    There are 9 roses and 9 daisies.

    Hope someone can help, thanks

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

    Lightbulb Something to try ...

    Can you modify this to your needs?
    Code:
    <html>
    <head>
    <title>Max of 2 arrays</title>
    <script type="text/javascript">
    // From: http://www.codingforums.com/showthread.php?t=198539
    
    var flower = ["roses", "violets", "buttercups", "daisies"];
    var flowerAmounts = [9, 8, 3, 9];
    var arr = [];
    for (var i=0; i<flower.length; i++) {
      arr.push(flowerAmounts[i]+','+flower[i]);  
    }
    arr.sort().reverse();
    // alert(arr.join('\n'));
    var maxValue = 0;
    var tmp = [];
    for (var i=0; i<arr.length; i++) {
    	tmp = arr[i].split(',');
    	if (tmp[0] > maxValue) { maxValue = tmp[0]; }
    }
    var str = 'The maximum amount of flowers is '+maxValue+'\n';
    for (var i=0; i<arr.length; i++) {
    	tmp = arr[i].split(',');
    	if (tmp[0] == maxValue) { str += arr[i]+'\n'; }
    }
    alert(str);
    </script>
    </head>
    <body>
    
    </body>
    </html>
    Sort of convoluted and not the way I would approach it, but it works...

    Good Luck!

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Another approach:-

    Code:
    <script type = "text/javascript">
    
    function sortBy(prop){return function(a,b){return a[prop]>b[prop] ? 1 : a[prop]<b[prop] ? -1 : 0; }}
    
    var myArray = [{"name":"Roses","qty":9},{"name":"Violets","qty":8}, {"name":"Buttercups","qty":"3"}, {"name":"Daisies","qty":"9"}];
    var len = myArray.length;
    document.write("Original Data <br>");
    for (var i = 0; i<=len-1; i++) {
    document.write (myArray[i].name + " " + myArray[i].qty + "<br>");
    }
    document.write ("<br>");
    document.write("Sorted By Name <br>");
    myArray.sort(sortBy("name"));
    for (var i = 0; i<=len-1; i++) {
    document.write (myArray[i].name + " " + myArray[i].qty + "<br>");
    }
    document.write ("<br>");
    document.write("Sorted By Quantity <br>")
    myArray.sort(sortBy("qty"));
    for (var i = 0; i<=len-1; i++) {
    document.write (myArray[i].name + " " + myArray[i].qty + "<br>");
    }
    document.write ("<br>");
    
    var x = myArray[len-1].qty;
    document.write("The flower(s) with the highest numbers are:-" + "<br>")
    for (var i =0; i<=len-1; i++) {
    if (myArray[i].qty == x) {
    document.write(myArray[i].name + " " + myArray[i].qty + "<br>");
    }
    }
    </script>
    "I used to think I was poor. Then they told me I wasn't poor, I was needy. Then they told me it was self-defeating to think of myself as needy. I was deprived. (Oh well, not deprived but rather underprivileged.) Then they told me that underprivileged was overused. I was disadvantaged. I still don't have a dime. But, I have a great vocabulary." - Jules Feiffer, cartoonist.
    Last edited by Philip M; 06-21-2010 at 09:30 AM.

  • #4
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks to both of you!

    Is there any way of achieving the same result without using functions? i.e just using an if loop? Reason being we haven't started with functions yet, i don't want to appear to know more than i do else they may expect to much from me in the future...

    Here's what i have so far... If it's right or not is a different matter all together

    var flower["roses", "violets", "buttercups", "daisies"];
    var flowerAmounts[9, 8, 3, 9];
    var max1;
    va max2;

    max1 = flowerAmounts[0];

    for (var i = 0; i < flowerAmounts.length;i = i++)
    {
    if (flowerAmounts[1] > max1)
    {
    max1 = flowerAmount[i];
    }
    }
    document.write('The maximum amount of flowers is ' + max + '<BR>');
    document.write('There are ' + max1[flower]+ 'and ' + max2[flower] + '<BR>');

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    More homework! Your teachers will always have a rose-tinted view of your abilities if we keep doing your homework for you. Remember that you cannot expect to go through life asking others to do your work for you.


    Code:
    <script type = "text/javascript">
    var flower = ["roses", "violets", "buttercups", "daisies"];
    var flowerAmounts = [9, 8, 3, 9];
    var largest = [];
    var len = flower.length;
    var max = -99;
    var count = 0;
    
    for (var i =0; i< len; i++){
    if (flowerAmounts[i] > max) {
    max = flowerAmounts[i];
    }
    }
    
    for (var i = 0; i< len; i++){
    if (flowerAmounts[i] == max) {
    largest[i] = flower[i];
    count++;
    }
    }
    
    largest.sort();
    
    document.write("The largest value(s):- <br>");
    for (var i = 0; i<=count-1; i++) {
    document.write(max + " " + largest[i] + "<br>")
    }
    
    </script>
    Last edited by Philip M; 06-21-2010 at 11:09 AM.

  • #6
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    jeje - my point exactly!

    The first two parts of the code make sense, i still have to somehow get around using

    largest.sort();

    Have just tried (off the top of my not very good head) to switch largest.sort() to a for loop the second value turns to undefined and the script keeps looping...

    Have also tried to find a code breakdown of largest.sort() case it gave me any ideas but no luck.

    Must admit, i enjoy javascript but arrays have thrown me into confusion, how long does it take for this to make any sense?

    Thanks again

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by confusedinjava View Post
    The first two parts of the code make sense, i still have to somehow get around using
    largest.sort();
    The sort was not really necessary.


    Code:
    document.write("The largest value(s):- <br>");
    for (var i = 0; i< len; i++) {
    if (flowerAmounts[i] == max) {
    document.write(max + " " + largest[i] + "<br>")
    }
    }

  • Users who have thanked Philip M for this post:

    confusedinjava (06-21-2010)

  • #8
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Thanks

    Thanks

    As way of explaining myself, i had spent 3 days adapting all the exercises i have been given to see if any would work, none did. I also re-read the info on Arrays, and still no joy... I used the forum as a last resort because at this point i wanted to understand it more than i wanted to complete the exercise.

    Once again, i really do appreciate your help


  •  

    LinkBacks (?)


    Posting Permissions

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