DEV Community

loading...

Discussion on: Daily Challenge #5 - Ten Minute Walk

Collapse
kesprit profile image
kesprit

My solution in Swift language :

func generateMinutesWalk(minutes: Int) -> [String]? {
    guard minutes % 2 == 0 else {
        print("Minutes is odd")
        return nil
    }

    let coordinates = ["N", "O", "S", "E"]
    var travel = [String]()

    for _ in 0..<minutes / 2 {
        travel.append(coordinates[Int.random(in: 0..<coordinates.count)])
    }
    travel.forEach { value in
        if let index = coordinates.firstIndex(of: value) {
            travel.append(coordinates[(index + 2) % coordinates.count])
        }
    }

    return travel
}