var array = [3, 5, 6, 9, 12,15, 14, 56, 7, 8, 65, 78];
var largest = array[0];
for( var i = 0; i <= array.length; i++){
if(largest < array[i]){
var largest = array[i];
}
}
console.log(largest);
Find largest number from array by function in javascript
function largestElements(numbers) {
var largest = 0;
for (let i = 0; i < numbers.length; i++) {
var elements = numbers[i];
if (elements > largest) {
largest = elements;
}
}
return largest;
}
const numbers = [2, 3, 4, 8, 5, 2, 4];
console.log(largestElements(numbers));
//Output: 8