dimanche 20 septembre 2020

Can someone explain how this for loop works?

This function returns the largest number from an array. I need help on understanding the if part: if (arr[i] > maxNumber) {maxNumber = arr[i]}. Using pseudocode or an explanation how exactly does this work?

function max(arr){
    let maxNumber = 0
    for (i = 0; i < arr.length; i++) {
        if (arr[i] > maxNumber){
            maxNumber = arr[i]
        }
    }
    return maxNumber
}

console.log(max([1,2,3,40,5]));

Aucun commentaire:

Enregistrer un commentaire