
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="utf8">
<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>

I suggest you take a look here:
http://www.javascriptkit.com/javatutors/arraysort.shtml
This covers sorting arrays and would probably help you out.

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().

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>

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.

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.

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
}

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>

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?

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.

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.

if (numbersArrray[i] > maximum) { maximum = numbersArrray[i]; }

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>

It is too possible without visible loops :
Code:
var num = [8,43,23,54,9];
var max=num.sort(function(a,b){return ab}).pop();
alert(max);

Originally Posted by pholder
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?
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

Forum Rules

