I am trying to find and return the largest number in each array using a for loop but every time I try to print something I get an error. I have tried using the following JS array methods: Math.max, Math.max.apply but I cannot get them to work.
My code is below. Any help will be appreciated and if you could please point out what I am doing wrong, that would be really great. Thanks in advance!
Question: numbers is an array of integers. Find and return the largest integer.
My code:
function largestNumber(numbers) { // this function was given by the quiz writer, my code starts below "code here"
// code here
numbers = [1, 2, 3, 4, 5];
for(let i = 0; i < numbers.length; i++){
let largest = 0;
if(numbers[i] > largest){
largest = largest.numbers[i];
}
}
return largest;
}
console.log(largestNumber);