Some ES6 magic for you, using the spread syntax:
function biggestNumberInArray(arr) {
const max = Math.max(...arr);
return max;
}
Actually, a few people have answered this question in a more detailed fashion than I do, but I would like you to read this if you are curious about the performance between the various ways of getting the largest number in an array.
Answer from wentjun on Stack OverflowSome ES6 magic for you, using the spread syntax:
function biggestNumberInArray(arr) {
const max = Math.max(...arr);
return max;
}
Actually, a few people have answered this question in a more detailed fashion than I do, but I would like you to read this if you are curious about the performance between the various ways of getting the largest number in an array.
There are multiple ways.
- Using Math max function
let array = [-1, 10, 30, 45, 5, 6, 89, 17];
console.log(Math.max(...array))
- Using reduce
let array = [-1, 10, 30, 45, 5, 6, 89, 17];
console.log(array.reduce((element,max) => element > max ? element : max, 0));
- Implement our own function
let array = [-1, 10, 30, 45, 5, 6, 89, 17];
function getMaxOutOfAnArray(array) {
let maxNumber = -Infinity;
array.forEach(number => { maxNumber = number > maxNumber ? number : maxNumber; })
console.log(maxNumber);
}
getMaxOutOfAnArray(array);
Videos
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);
let arr = ["a", 3, 5, 8, 100, 20];
let max = Math.max(...arr.filter(e => typeof e !== "string"));
console.log(max);
OR
let arr = ["a", 3, 5, 8, 100, 20];
let max;
for (item of arr) {
if (typeof item != "string") {
if (max) {
if (max < item) max = item;
} else {
max = item;
}
}
}
console.log(max);
If you NEED to use a loop, then u shouldn't use Math.max() as it takes an undefined list of parameters (not an array, and no other types than numbers).
You can still manage to filter the array beforehand or check the type.
The error you do is that you use the function Math.max() in a loop that already "works as a loop"
Here is the spec :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
A simple way to do a loop is :
max = 0
for(item in array2){
if(item>max){
max=item
}
}
console.log(max)
edit: you can check with typeof to avoid errors and initiate the value of max with -Infinity or else you can't get the max out of negative numbers
The line largest = array[i]; does NOT mean 0 = array[i] as you believe. largest is a variable, so each time you get to the line largest = array[i]; you are changing the value of largest to be the current array[i].
This is why you get the max of the array at the end.
An example: a = [1, 3, 7, 2]
You initialize largest = 0. The, for each element of the array you do the following:
largest < 1? yes, so largest = 1
largest < 3? yes, so largest = 3
largest < 7? yes, so largest = 7
largest < 2? no, so do nothing
var array = [3, 4, 5, 21.15, 21, 9];
var largest = 0;
for (i=0; i<array.length; i++) {
if (array[i]>largest) {
largest = array[i];
}
}
console.log(largest);
Iteration 1:
Check if array[0] i.e. 3 is greater than 0 => True : largest = 3
Iteration 2:
Check if array[1] i.e. 4 is greater than 3 => True : largest = 4
Iteration 3:
Check if array[2] i.e. 5 is greater than 4 => True : largest = 5
Iteration 4:
Check if array[3] i.e. 21.25 is greater than 5 => True : largest = 21.25
Iteration 5:
Check if array[4] i.e. 21 is greater than 21.25 => False: largest = 21.25
Iteration 6:
Check if array[5] i.e. 9 is greater than 21.25 => False: largest = 21.25
This will work
var arr = [2,3,4];
var largest = 0;
arr.forEach(function(elem){
if(largest < elem)
largest = elem;
});
console.log(largest);
but why would you even do that? Why not use Math max?
var largest = Math.max.apply(Math, arr);
To cover negative numbers in array, it is safe to assign largest to be the first element in array.
var arr = [2,3,4];
var largest = arr[0];
arr.forEach(function(elem){
if(largest < elem)
largest = elem;
});
console.log(largest);