Challenge: Preventing Repeat Answers

Hello guys I’d like to share my solution for the challenge mentioned in the title above if you see any issues or would like to improve my solution don’t hesitate please
(Note that my solution won’t fix rotation bug)
first i modified the Question model by adding answered property to become

data class Question(@StringRes val textResId: Int, val answer: Boolean,var answered: Boolean)

after that i set the default value for answered to false

private val questionBank = listOf(
        Question(R.string.question_australia, true,false),
        Question(R.string.question_oceans, true,false),
        Question(R.string.question_mideast, false,false),
        Question(R.string.question_africa, false,false),
        Question(R.string.question_americas, true,false),
        Question(R.string.question_asia, true,false)
    )

then i created the following function called isAnswered

private fun isAnswered(index:Int){
        if (questionBank[index].answered==true){
            trueButton.isEnabled=false
            falseButton.isEnabled=false
        }else{
            trueButton.isEnabled=true
            falseButton.isEnabled=true
        }
    }

after that I modified the nextButton listener and previousButton listener as you can see

nextButton.setOnClickListener {
            currentIndex = (currentIndex + 1) % questionBank.size
            isAnswered(currentIndex)
            updateQuestion()
        }

previousButton.setOnClickListener {
            currentIndex = (currentIndex - 1) % questionBank.size
            if (currentIndex == -1) currentIndex = questionBank.lastIndex
            isAnswered(currentIndex)
            updateQuestion()
        }

and finally I modified trueButton and falseButton to become

trueButton.setOnClickListener {
            trueButton.isEnabled=false
            falseButton.isEnabled=false
            questionBank[currentIndex].answered=true
            checkAnswer(true)
        }

falseButton.setOnClickListener {
            trueButton.isEnabled=false
            falseButton.isEnabled=false
            questionBank[currentIndex].answered=true
            checkAnswer(false)
        }

I’d like to hear your thoughts about the solution above :slight_smile:

This is essentially what needs to be done and accomplishes the challenge. Some possible modifications that could be made are:

First, when creating the Question data class, note how every question is always initializing the answered property to be false. We can set the default value for the 3rd parameter to be false.

data class Question(@StringRes val textResId: Int, 
                    val answer: Boolean,
                    var answered: Boolean = false)

and then nothing changes when creating the question bank, which is nice.

private val questionBank = listOf(
    Question(R.string.question_australia, true),
    Question(R.string.question_oceans, true),
    Question(R.string.question_mideast, false),
    Question(R.string.question_africa, false),
    Question(R.string.question_americas, true),
    Question(R.string.question_asia, true)
)

Next, inside of isAnswered() since the answered property is already a boolean value, we don’t need to check if it equals true and have the if/else structure. We can just set the enabled property to opposite the answered property

private fun isAnswered(index: Int) {
    val isQuestionAnswered = questionBank[index].answered
    trueButton.isEnabled = !isQuestionAnswered
    falseButton.isEnabled = !isQuestionAnswered
}

Finally, I would move the code in each click listener in to the checkAnswer method to prevent code duplication. Specifically the lines

trueButton.isEnabled = false
falseButton.isEnabled = false
questionBank[currentIndex].answered = true

Take notice that these three lines are in both click listeners and the same for both buttons.

// After the user answers a question, disable True and False buttons     
  private fun isAnswered() {
                falseButton.isEnabled = false
                trueButton.isEnabled = false
            }  

 // After the user presses the Next button enable them
  private fun refreshButtons() {
            trueButton.isEnabled = true
            falseButton.isEnabled = true

nextButton.setOnClickListener {
            currentIndex = (currentIndex +1) % questionBank.size
            refreshButtons()  // <---
            updateQuestion()
        }

falseButton.setOnClickListener { view : View ->
  checkAnswer(false)
  isAnswered() // <---
}

 trueButton.setOnClickListener { view : View ->
        checkAnswer(true)
        isAnswered()  // <---

}

I am happy with this simple solution.

This also works. It depends if you want to lock out the current question from repeat answers OR if you want to lock out the specific question from repeat answers (hitting next then previous still disables the buttons).

The challenge doesn’t specify which solution it is looking for.