What would be the proper Syntax

I'm relatively new to this and I am having and small issue in where to place the return in my scopes and basically figuring out the proper syntax for this.

func isPassingGrade(for scores: [Int]) -> Bool {

    var total = 0

    for score in scores {

  total += score

    if total >= 500 {

        return true

        }else {

        return false

}

    }

}

func isPassingGrade(for scores: [Int]) -> Bool {
    var total = 0
    for score in scores {
        total += score
    }
    return total >= 500
}

In addition, if you want to return as soon as you have check that total is over 500, you can:

func isPassingGrade(for scores: [Int]) -> Bool {
    var total = 0
    for score in scores {
        total += score
        if total >= 500 { return true }  // <<-- Add this
    }
    return total >= 500  // Then this will always return false
}

Or if you want to skip ahead to more advanced Swift usage (without the early return optimization), consider this:

func isPassingGrade(for scores: [Int]) -> Bool {
    scores.reduce(0, +) >= 500
}

There’s no explicit sum() function in the Swift library but this reduce() expression is equivalent. And reduce() is well worth getting familiar with.

What would be the proper Syntax
 
 
Q