CodeBlanks Logo Icon CodeBlanks

Putting it together

Combine the quiz functions with a menu loop.

Putting it together

In the fifth and final lesson we combine all pieces with a menu loop. The runMenu function takes a list of actions and processes each one in order.

Example: fun runMenu(actions: List<String>) { for (action in actions) { when (action) { "quiz" -> runQuiz(); "score" -> println(score(listOf("Ankara", "5"))); else -> println("Unknown") } } }. With when each action type maps to a function call. In main questions are added, then runMenu(listOf("quiz", "score")) is called. All functions (Question, addQuestion, runQuiz, score) come from the previous lessons.

Menu loop

runMenu takes a List<String> and processes each action in order. With when it calls the matching function for each action type.

Example: runMenu(listOf("quiz", "score")) first calls runQuiz() (prints all questions), then println(score(listOf("Ankara", "5"))) (prints the score). Unknown actions go to the else -> println("Unknown") branch. The menu loop uses a fixed action list instead of user input, so the output is deterministic.

Practice Combine the quiz with runMenu

Build on the previous lessons' solution: data class Question(val text: String, val answer: String), val questions: MutableList<Question> = mutableListOf(), fun addQuestion(text: String, answer: String), fun runQuiz() and fun score(answers: List<String>): Int are defined. Add a fun runMenu(actions: List<String>) function whose body iterates actions with for (action in actions) and uses when (action) to call runQuiz() for "quiz", println(score(listOf("Ankara", "5"))) for "score", and println("Unknown") for else. In main add two questions and call runMenu(listOf("quiz", "score")).

Starter code

Main.kt
data class Question(val text: String, val answer: String)

val questions: MutableList<Question> = mutableListOf()

fun addQuestion(text: String, answer: String) {
    questions.add(Question(text, answer))
}

fun runQuiz() {
    for (q in questions) {
        println(q.text)
        println("Answer: ${q.answer}")
    }
}

fun score(answers: List<String>): Int {
    var correct = 0
    for (i in questions.indices) {
        if (questions[i].answer == answers[i]) correct++
    }
    return correct
}

// add the runMenu function: takes actions, iterates with for, branches with when

fun main() {
    // add two questions and call runMenu
}

Sample solution

Main.kt
data class Question(val text: String, val answer: String)

val questions: MutableList<Question> = mutableListOf()

fun addQuestion(text: String, answer: String) {
    questions.add(Question(text, answer))
}

fun runQuiz() {
    for (q in questions) {
        println(q.text)
        println("Answer: ${q.answer}")
    }
}

fun score(answers: List<String>): Int {
    var correct = 0
    for (i in questions.indices) {
        if (questions[i].answer == answers[i]) correct++
    }
    return correct
}

fun runMenu(actions: List<String>) {
    for (action in actions) {
        when (action) {
            "quiz" -> runQuiz()
            "score" -> println(score(listOf("Ankara", "5")))
            else -> println("Unknown")
        }
    }
}

fun main() {
    addQuestion("Capital city?", "Ankara")
    addQuestion("2+2?", "4")
    runMenu(listOf("quiz", "score"))
}

Expected output

Capital city?
Answer: Ankara
2+2?
Answer: 4
1

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does the <code>runMenu</code> function use to process actions?

  • if-else if
  • when (action) Correct answer
  • try-catch
  • recursion

Explanation: when binds each action to a function.

2 Match the runMenu parts

Term Meaning
fun runMenu(actions) Function signature
for (action in actions) Action loop
when (action) Branching
else -> println Unknown branch

3 Complete the runMenu branching

Question
for (action in actions) {
    ____ (action) {
        "quiz" -> runQuiz()
        "score" -> println(score(listOf("Ankara", "5")))
        else -> println("Unknown")
    }
}
Answer
for (action in actions) {
    when (action) {
        "quiz" -> runQuiz()
        "score" -> println(score(listOf("Ankara", "5")))
        else -> println("Unknown")
    }
}

Explanation: when branches on the action.

All lessons in Intermediate