100 Days of SwiftUI

Day 31

2026-05-19

Project 5, part 3

Quiz: 11/12

Challenge

  1. Disallow answers that are shorter than three letters or are just our start word.

I tried to first add a guard condition to the addNewWord() function

guard answer.count < 3 else {
    wordError(title: "Word too short", message: "You need at least 3 characters")
    return
}

And that didn’t work, so I created a function isTooShort which had the same effect. Then I tried different conditions, e.g. answer.count == 1 and realized I was going the wrong direction. I decided a separate function was overkill so I put it back in addNewWord()

I have been doing so many logic puzzles lately that I often get false and true going the wrong way.

So the final is

guard answer.count > 2 else {
    wordError(title: "Word too short", message: "You need at least 3 characters")
    return
}
  1. Add a toolbar button that calls startGame(), so users can restart with a new word whenever they want to.

This was easier, but I did have to look up syntax. I hope one day I can at least remember what is supposed to be in there

It goes after the .navigationTitle(rootWood) modifier

.toolbar { ToolbarItem { Button("New Word") {startGame()} } }

I added usedWords = [] to startGame()

  1. Put a text view somewhere so you can track and show the player’s score for a given root word. How you calculate score is down to you, but something involving number of words and their letter count would be reasonable.

I’m only tracking how many words were found, and I’m putting it in the toolbar

.toolbar {
    ToolbarItem(placement: .topBarLeading) {
        Button("New Word") {startGame()}
    }
    ToolbarItem() {
        Text("\(usedWords.count) found")
            .fixedSize(horizontal: true, vertical: false)
    }.sharedBackgroundVisibility(.hidden)
}

I had to add the modifiers to the Text in order to make it big enough and to keep it from acting like a button (thanks, Reddit!)