Copyvar arr = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = arr[0];
for (var i = 0; i < arr.length; i++) {
if (arr[i] > largest ) {
largest = arr[i];
}
}
console.log(largest);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
- You need to define
ior else it become a global variable. - Don't redefine largest in the loop.
- Since you're looping through the array, use
i < array.lengthinstead ofi <= largest. - Since you're comparing each of the items in the array to
largest, useif(largest < array[i])instead ofif(array > largest) - You should set largest equal to the first element in the array because what if all the numbers are negative?
arrayis a bad variable name because it's too similar toArray(the array constructor). Tryarrinstead.
One liner:
var largest = Math.max.apply(0, array);
More info here: Javascript max() function for 3 numbers
Answer from Larry Battle on Stack OverflowVideos
Copyvar arr = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = arr[0];
for (var i = 0; i < arr.length; i++) {
if (arr[i] > largest ) {
largest = arr[i];
}
}
console.log(largest);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
- You need to define
ior else it become a global variable. - Don't redefine largest in the loop.
- Since you're looping through the array, use
i < array.lengthinstead ofi <= largest. - Since you're comparing each of the items in the array to
largest, useif(largest < array[i])instead ofif(array > largest) - You should set largest equal to the first element in the array because what if all the numbers are negative?
arrayis a bad variable name because it's too similar toArray(the array constructor). Tryarrinstead.
One liner:
var largest = Math.max.apply(0, array);
More info here: Javascript max() function for 3 numbers
Copyvar array = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = 0;
for (let i=0; i < array.length; i++) {
if (array[i] > largest) {
largest = array[i];
}
}
console.log(largest);
How about augmenting the built-in Array object to use Math.max/Math.min instead:
Array.prototype.max = function() {
return Math.max.apply(null, this);
};
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
let p = [35,2,65,7,8,9,12,121,33,99];
console.log(`Max value is: ${p.max()}` +
`\nMin value is: ${p.min()}`);
Here is a JSFiddle.
Augmenting the built-ins can cause collisions with other libraries (some see), so you may be more comfortable with just apply'ing Math.xxx() to your array directly:
var min = Math.min.apply(null, arr),
max = Math.max.apply(null, arr);
Alternately, assuming your browser supports ECMAScript 6, you can use spread syntax which functions similarly to the apply method:
var min = Math.min( ...arr ),
max = Math.max( ...arr );
var max_of_array = Math.max.apply(Math, array);
For a full discussion see: http://aaroncrane.co.uk/2008/11/javascript_max_api/
I'm trying to get the maximum value in an array. What's the correct syntax to do this?
Alternative, is there a way to compare different variables and get the name of the variable with the highest value? Like
var memory = 10 var cores = 20 var level = 200 var node = 5 -> return "level"