I just wanted to know if there is a more basic solution with what we learn until now (Lecture Loops) or if someone just found another way to do this and wants to share:
★ SOLUTION Nº1
export const maxElement = (arr) => {
let biggest = -Infinity;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > biggest) {
biggest = arr[i];
}
}
return biggest;
}
★ SOLUTION Nº2
export const maxElement = (arr) => {
let maxi = Math.max(...arr);
return maxi;
}
★ SOLUTION Nº3
export const maxElement = (arr) => {
return Math.max.apply(null, arr);
}
★ SOLUTION Nº4
export const maxElement = (arr) => {
return arr.reduce((a, b) => Math.max(a, b));
}
★ SOLUTION Nº5
export const maxElement = (arr) => {
return arr.reduce(function (a, b) {
return (a > b ? a : b);
});
}
★ SOLUTION Nº6
export const maxElement = (arr) => {
let len = arr.length, max = -Infinity;
while (len--) {
if (arr[len] > max) {
max = arr[len];
}
}
return max;
}