I got trouble using a function within a for loop inside. First look at the code please:
let list1 = [4168,3925,858,2203,440,185,2886,160,1811,4272,4333,2180,174,157,361,1555];
let max = 0;
function maximum(listNumb1) {
for (let i = 0; i < listNumb1.length; i++) {
if (listNumb1[i] > max) {
max = listNumb1[i];
return max;
}
}
}
console.log(maximum(list1));
Ok, I am trying to make function that will find the biggest number in the array above. I made it without a function, and code works, but when I want to make a function, so I can use it with multiple arrays, I am stuck and I am not getting the desired output.
I know where my problem is. It's the return part. Its stopping the code from executing further and I am getting the first number of the array as a output because the for loop stops after the return.
I am asking for solution how should I end, or what should I use instead of return, so I wont break the looping, and the function will continue iterating trough the rest of the numbers in the array, and the max variable will change until it finds the biggest number in the array?