WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Guess the Flag bonus challenge fatal error? (Day 22)

Forums > 100 Days of SwiftUI

Hi there,

I have just finished the Guess the Flag challenge, and implemented the functionality to remove repeats - adding the static let array of allCountries, implemented into func askQuestion() and newGame(). However, when i try to play the game, it gives me a fatal error.

It is because that running newGame() it loops back to askQuestion() and then immediately removes a country, so that a new game starts with only 9 countries. The fatal error appears when button label asks for a value that is out of bounds of the countries array.

Am I the only one that experience this problem, or is it someting in my code, that is wrong?

    func askQuestion() {
        countries.remove(at: correctAnswer)
        correctAnswer = Int.random(in: 0...2)
        questionCounter += 1

    func newGame() {
        questionCounter = 0
        score = 0
        countries = Self.allCountries


@Frederick may have lost track of his logic!

Am I the only one that experience this problem, or is it someting in my code, that is wrong?

Sometimes it's worthwhile to grab a pencil and a scrap of paper and write down the steps you're giving to your compiler.

From your code above...

func newGame() {
        questionCounter = 0                     // 1. Reset the counter
        score = 0                               // 2. Reset the score
        countries = Self.allCountries           // 3. Grab a fresh copy of ALL the countries

func askQuestion() {
        countries.remove(at: correctAnswer)     // 4. Remove the country at position correctAnswer
        countries.shuffle()                     // 5. Shuffle the remaining countries
        correctAnswer = Int.random(in: 0...2)   // 6. Pick one of the first three countries
        questionCounter += 1                    // 7. something

Your logic:

  1. Previous game's correct answer was New Zealand in array position 1. (This is an example.)
  2. Reset the game's question counter to zero.
  3. Reset the game's score to zero.
  4. Grab a fresh copy of ALL the countries.
  5. Remove the country at position 1. (From step 0)
  6. Shuffle the remaining countries.
  7. Pick one of the first three countries.

For illustration purposes, let's assume in the previous game that you shuffled the array and that New Zealand was in position #1 and was selected as the correct answer.

Please note Step 3! When you get a FRESH copy of ALL your countries, where is New Zealand in this copy? Can you be sure that it's in the first position?

Then in Step 4, you REMOVE whatever country is in position #1. (It might be Greenland! or Madagascar!)

Back to the Drawing Board

I didn't answer your question directly. But this should put you on the right path to restructuring your logic.


Hi @Obelix,

Thank you very much for your help - I will certainly remember to use your handy trick (pen and paper) when I get stuck once again.

However, I am still a bit stuck as I have gone through Paul's solution material for the challenge, and all code he has provided for both func askQuestion() and func newGame() is the same as mine. I have also tried to move around with countries.remove(at: correctAnswer) and countries = Self.allCountries to rearrange the order of execution.

I could add the necessary functionality directly to newGame(). Then I don't need to call askQuestion() and accidently remove(at: X).

proposed newGame function

    func newGame() {
        questionCounter = 1
        score = 0
        countries = Self.allCountries.shuffled()
        correctAnswer = Int.random(in: 0...2)


Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.