www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Finding a maximum inside an array using loops

  1. #1
    Join Date
    Jul 2012
    Posts
    28

    Finding a maximum inside an array using loops

    Hello,

    I'm taking an introductory javascript course and I'm want to figure out how to find the maximum value inside an array using a for loop.

    What am I doing wrong and how can I fix it?

    This is what I have so far:

    <!doctype html>
    <html lang ="en">
    <head>
    <title> Array </title>
    <meta charset="utf-8">
    <script>
    var numbersArray = [8, 43, 23, 54, 9];
    for (var i = 0; i < numbersArray.length; i++) {
    var numbers = Math.max.apply numbersArray[i];
    numbersArray[i] = numbers;
    }
    console.log(numbersArray);
    </script>
    </head>
    <body>
    </body>
    </html>

  2. #2
    Join Date
    Jun 2012
    Location
    Dark Side of the Moon
    Posts
    70
    I suggest you take a look here:

    http://www.javascriptkit.com/javatutors/arraysort.shtml

    This covers sorting arrays and would probably help you out.

  3. #3
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773
    I think the point of the exercise is to learn how to loop over an array and compare values.

    Using Math.max could get you what you want, however this is the incorrect way to use it. Instead, use it this way:
    Code:
    var maxValue = Math.max.apply(Math, numbersArray);
    I suspect this isn't what the class exercise is aiming for. Instead, you want to loop over all the numbers in the array. You need to keep track of which number is currently largest in another variable. There is no need to Math.max().

  4. #4
    Join Date
    Jul 2012
    Posts
    28
    this is what i have so far for this loop, hopefully I'm going in the right direction. So far it's just returning all the values. How can I make it return the biggest one using the for loop?

    <script>
    var maximumValue = [4, 3, 5, 1, 7];
    var maxValue = Math.max.apply(Math, numbersArray);
    for (var i = 0; i < maximumValue.length; i++) {
    var max = maximumValue[i];
    maximumValue[i] = maximum;
    }

    console.log(maximumValue);
    </script>

  5. #5
    Join Date
    Jan 2009
    Posts
    3,346
    You need to compare the value from the current loop against the highest value you found so far...

    So:

    //start loop
    //is X larger than Y?
    // if so, set Y to X so Y will always contain the highest value
    //continue loop unless end of loop

    //Y will be the largest value in the loop



    Comparison operators include equal to (==) greater than (>) less than (<) not equal to (!=) and several others you will probably run into eventually.

  6. #6
    Join Date
    Jul 2012
    Posts
    28
    Quote Originally Posted by criterion9 View Post
    You need to compare the value from the current loop against the highest value you found so far...

    So:

    //start loop
    //is X larger than Y?
    // if so, set Y to X so Y will always contain the highest value
    //continue loop unless end of loop

    //Y will be the largest value in the loop



    Comparison operators include equal to (==) greater than (>) less than (<) not equal to (!=) and several others you will probably run into eventually.
    could you use an example, I'm a little confuse about what that means.

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    So using the comments I posted already you could walk through each iteration of code something like:


    Code:
    var numbersArray = [8, 43, 23, 54, 9]; 
    var maximum = 0; //we start with a maximum of zero since all the numbers in the array are positive numbers greater than 0
    for (var i = 0; i < numbersArray.length; i++) { 
    //you need to compare the current number from the array to your current maximum
    //for that you need a conditional statement
    //if the current number numbersArray[i] is greater than maximum we have a new maximum
    //in the first iteration numbersArray[i] will be 8 and maximum will be 0 because we haven't changed it
    }
    Does that help?


    A conditional statement in Javascript is structured like:
    Code:
    if(condition){
      //this code is executed if the condition is true
    }

  8. #8
    Join Date
    Jul 2012
    Posts
    28
    thanks for the help. this is what i got so far. however, i keep getting 1 as the answer even though there are other higher values.

    <script>
    var numbersArray = [1, 5, 4, 3, 6];
    var maximum = 0;
    for (var i = 0; i < numbersArray; i++) {
    if (numbersArrray[i] > maximum) {Math.max}
    }
    console.log("Max: " + numbersArray[i]);
    </script>

  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by pholder View Post
    thanks for the help. this is what i got so far. however, i keep getting 1 as the answer even though there are other higher values.

    <script>
    var numbersArray = [1, 5, 4, 3, 6];
    var maximum = 0;
    for (var i = 0; i < numbersArray; i++) {
    if (numbersArrray[i] > maximum) {Math.max}
    }
    console.log("Max: " + numbersArray[i]);
    </script>
    You are missing the part that reassigns the highest value to maximum.

    i.e. maximum = theHigherNumber

    Not sure what you have Math.max in there for. Is that part of the assignment?

  10. #10
    Join Date
    Jul 2012
    Posts
    28
    the point of the assignment is to find the biggest value in an array using a for loop, so i guess using Math.max is not necessary.

  11. #11
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by pholder View Post
    the point of the assignment is to find the biggest value in an array using a for loop, so i guess using Math.max is not necessary.
    Nope. Math.max is the shortcut to doing it with a loop. You will need to pull out the Math.max part and instead assign the higher value to the maximum variable.

  12. #12
    Join Date
    May 2004
    Location
    Manhattan NY
    Posts
    6,028
    if (numbersArrray[i] > maximum) { maximum = numbersArrray[i]; }

  13. #13
    Join Date
    Jul 2012
    Posts
    28
    This is what i got with some comments added.

    <script>
    var numbersArray = [5, 6, 4, 1, 3];
    var maximum = 0;

    /* i set the current maximum to zero so that any number greater than
    0 will give us a new maximum */

    /* the for loop starts at 0 and goes all the way to the end of the array with
    the length property. then the variables are set equal to each other */

    for (var i = 0; i < numbersArray.length; i++)
    {maximum = numbersArray;

    /* the if statments says that if the numbers in the array are larger than the established maximum,
    then the next highest number will be the new maximum */
    if (numbersArray[i] > maximum) { maximum = numbersArray[i]; }
    }

    /* console.log to show the maximum */

    console.log(maximum);

    </script>

  14. #14
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    It is too possible without visible loops :
    Code:
    var num = [8,43,23,54,9];
    var max=num.sort(function(a,b){return a-b}).pop();
    alert(max);

  15. #15
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by pholder View Post
    This is what i got with some comments added.

    <script>
    var numbersArray = [5, 6, 4, 1, 3];
    var maximum = 0;

    /* i set the current maximum to zero so that any number greater than
    0 will give us a new maximum */

    /* the for loop starts at 0 and goes all the way to the end of the array with
    the length property. then the variables are set equal to each other */

    for (var i = 0; i < numbersArray.length; i++)
    {maximum = numbersArray;

    /* the if statments says that if the numbers in the array are larger than the established maximum,
    then the next highest number will be the new maximum */
    if (numbersArray[i] > maximum) { maximum = numbersArray[i]; }
    }

    /* console.log to show the maximum */

    console.log(maximum);

    </script>
    Just out of curiosity...what do you think this line is going to do?
    maximum = numbersArray;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles