Kotlin / kotlin-koans

Kotlin workshop
MIT License
2.6k stars 1.41k forks source link

Conventions : For loop - task fails to run on https://try.kotlinlang.org #153

Closed nieldw closed 6 years ago

nieldw commented 6 years ago

There is an issue with this task:

https://try.kotlinlang.org/#/Kotlin%20Koans/Conventions/For%20loop/Task.kt

Trying to 'Check' the solution produces a 500, "SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data"

AlexanderPrendota commented 6 years ago

Hello! Please insert the solution:

class DateRange(val start: MyDate, val end: MyDate): Iterable<MyDate>{
    override fun iterator(): Iterator<MyDate> = DateIterator(this)
}

class DateIterator(val dateRange:DateRange) : Iterator<MyDate> {
    var current: MyDate = dateRange.start
    override fun next(): MyDate {
        val result = current
        current = current.nextDay()
        return result
    }
    override fun hasNext(): Boolean = current <= dateRange.end
}

fun iterateOverDateRange(firstDate: MyDate, secondDate: MyDate, handler: (MyDate) -> Unit) {
    for (date in firstDate..secondDate) {
        handler(date)
    }
}

works fine for me :)

Thanks.

nieldw commented 6 years ago

Thanks @AlexanderPrendota. It looks like there was something else going on, producing the 500s, it's working fine now.