# Thread: Finding a maximum inside an array using loops

1. Registered User
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">
<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>
<body>
</body>
</html>

2. Registered User
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

3. 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. Registered User
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. MPM, PMP, CSM, BS CIS
Join Date
Jan 2009
Location
Ohio
Posts
3,356
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. Registered User
Join Date
Jul 2012
Posts
28
Originally Posted by criterion9
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. MPM, PMP, CSM, BS CIS
Join Date
Jan 2009
Location
Ohio
Posts
3,356
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. Registered User
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. MPM, PMP, CSM, BS CIS
Join Date
Jan 2009
Location
Ohio
Posts
3,356
Originally Posted by pholder
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. Registered User
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. MPM, PMP, CSM, BS CIS
Join Date
Jan 2009
Location
Ohio
Posts
3,356
Originally Posted by pholder
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. if (numbersArrray[i] > maximum) { maximum = numbersArrray[i]; }

13. Registered User
Join Date
Jul 2012
Posts
28

<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. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
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();

15. MPM, PMP, CSM, BS CIS
Join Date
Jan 2009
Location
Ohio
Posts
3,356
Originally Posted by pholder

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