[ad_1]
I am struggling to workout how this code is working, how is the maxNum part working? maxNum[0] < numbers[i] how is it grabbing the last number from the array?
function sortThem(numbers) {
let minNum = numbers[0]
let maxNum = numbers[0]
for (let i = 0; i < numbers.length; i++) {
if (minNum > numbers[i]) {
minNum = numbers[i]
} else if (maxNum < numbers[i]) {
maxNum = numbers[i]
}
}
console.log(maxNum)
const minMax = [minNum, maxNum]
return minMax
}
const results = sortThem([2, 9, 10, 17, 45])
console.log(results)
[ad_2]