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;
}
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>
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.
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.
<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]; }
}
<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?
Bookmarks