/**
 * This exercise is designed to test your understanding of using break and continue statements within a while loop.
 * You need to create a function that calculates the average speed of an athlete based on their completed laps.
 *
 * @param distance The total distance to be covered in the race.
 * @param timeLimit The maximum time limit for the race.
 * @return The average speed of the athlete, rounded to 2 decimal places.
 */
fun athleteStats(distance: Double, timeLimit: Double): Double {

    var totalDistance = 0.0
    var totalTime = 0.0
    var lapCount = 0

    while (true) {
        print("Enter the distance covered in the current lap (in meters): ")
        val lapDistance = readLine()!!.toDouble()
        print("Enter the time taken for the current lap (in seconds): ")
        val lapTime = readLine()!!.toDouble()

        if (lapDistance < 0 || lapTime < 0) {
            println("Invalid input! Distance and time values must be greater than or equal to 0.")
            continue
        }

        totalDistance += lapDistance
        totalTime += lapTime
        lapCount++

        if (totalDistance >= distance || totalTime >= timeLimit) {
            break
        }
    }

    return (totalDistance / totalTime).format(2).toDouble()
}

fun Double.format(digits: Int) = "%.${digits}f".format(this)
/**
 * This exercise is designed to test your understanding of using break and continue statements within a while loop.
 * You need to create a function that calculates the average speed of an athlete based on their completed laps.
 *
 * @param distance The total distance to be covered in the race.
 * @param timeLimit The maximum time limit for the race.
 * @return The average speed of the athlete, rounded to 2 decimal places.
 */
fun athleteStats(distance: Double, timeLimit: Double): Double {
/**
 * This exercise is designed to test your understanding of using break and continue statements within a while loop.
 * You need to create a function that calculates the average speed of an athlete based on their completed laps.
 *
 * @param distance The total distance to be covered in the race.
 * @param timeLimit The maximum time limit for the race.
 * @return The average speed of the athlete, rounded to 2 decimal places.
 */
fun athleteStats(distance: Double, timeLimit: Double): Double {

    var totalDistance = 0.0
    var totalTime = 0.0
    var lapCount = 0

    while (true) {
        print("Enter the distance covered in the current lap (in meters): ")
        val lapDistance = readLine()!!.toDouble()
        print("Enter the time taken for the current lap (in seconds): ")
        val lapTime = readLine()!!.toDouble()

        if (lapDistance < 0 || lapTime < 0) {
            println("Invalid input! Distance and time values must be greater than or equal to 0.")
            continue
        }

        totalDistance += lapDistance
        totalTime += lapTime
        lapCount++

        if (totalDistance >= distance || totalTime >= timeLimit) {
            break
        }
    }

    return (totalDistance / totalTime).format(2).toDouble()
}

fun Double.format(digits: Int) = "%.${digits}f".format(this)