From 8be28cd39a3ee3a642a047a8e303f70a24b1e5a4 Mon Sep 17 00:00:00 2001 From: John Long Date: Mon, 3 Jan 2022 16:45:26 -0800 Subject: [PATCH] A little cleaner implementation --- 85_Synonym/kotlin/Synonym.kt | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/85_Synonym/kotlin/Synonym.kt b/85_Synonym/kotlin/Synonym.kt index 94066d03..d2e68dc4 100644 --- a/85_Synonym/kotlin/Synonym.kt +++ b/85_Synonym/kotlin/Synonym.kt @@ -13,23 +13,25 @@ fun main() { println(introText) synonyms.forEach { - // Inside with, "this" is the current synonym - with(it) { - do { - val answer = ask(" WHAT IS A SYNONYM OF $word ? ") - when { - answer == "HELP" -> - println("""**** A SYNONYM OF $word IS ${synonyms.random()}.""") - synonyms.contains(answer) -> - println(RANDOM_ANSWERS.random()) - else -> - println("TRY AGAIN.") - } - } while (!synonyms.contains(answer)) - } + it.testUser() } println("SYNONYM DRILL COMPLETED.") } +// We could put this inside of SynonymList, but this keeps the core implementation +// right here at the top +private fun SynonymList.testUser() { + do { + val answer = ask(" WHAT IS A SYNONYM OF $word ? ") + when { + answer == "HELP" -> + println("""**** A SYNONYM OF $word IS ${synonyms.random()}.""") + synonyms.contains(answer) -> + println(RANDOM_ANSWERS.random()) + else -> + println("TRY AGAIN.") + } + } while (!synonyms.contains(answer)) +} val introText = """ ${tab(33)}SYNONYM