FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

Spot the error

This Swift code contains one error – can you select the line that is incorrect?

Tip: You should mentally run the code from top to bottom, meaning that if line 3 says an integer should be returned and line 10 tries to return a string, line 10 is the wrong one.

func performExercise(repetitions: Int, type: String) -> String {
    var returnValue: String = ""
    for _ in 0 ..< repetitions {
        returnValue += "I'm doing a \(type)\n"
    if repetitions < 10 {
        returnValue += "That was easy!"
    } else if repetitions < 30 {
        returnValue += "That was challenging!"
    } else {
        returnValue += "Phew - I need a break!"
    return returnValue
print(performExercise(repetitions: 42.5, type: "squat"))
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code
Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.