Chapter 2 Exercises — Swift Fundamentals
These exercises go with Chapter 2. Do the independent challenge on a Git branch so the guided project stays identical to the reference.
Independent Challenge — Expected total for 2D8 + 3
Goal
Add a playground block named Expected total for 2D8 + 3 to SwiftPractice.swift. It computes the expected total of rolling two eight-sided dice and adding 3, then prints one sentence with the result.
Requirements
- Use
letfor every value that does not change. - Store the average of one d8 as a
Double. The average of the numbers 1 through 8 is 4.5, so the value should be 4.5. - Compute the expected total of two dice by converting the number of dice to a
Doublebefore you multiply. - Add the modifier of 3.
- Print a sentence that includes the number of dice, the sides, the modifier and the total, using string interpolation.
- The printed total is
12.0. - The file builds with no errors and no warnings.
Acceptance criteria
- Running the new playground prints one line that ends with
12.0. - The new playground contains no
vardeclarations. - The
Double(...)conversion appears wherever anIntmeets aDouble. - You made the change on a branch named
challenge-02, and you switched back tomainafterward.
Setup
First commit and tag the guided work using the workbook's Development Checkpoint. Confirm git status --short prints nothing, then create the branch:
git switch -c challenge-02
When you finish:
Uncommitted edits can follow you across branches. Save them before switching; if nothing changed, skip add and commit. The status command should print nothing:
git add -A
git commit -m "Challenge 02: Save practice work"
git status --short
git switch main
Hint 1
Write the playground as a plain block with a name in quotation marks, just like the blocks in Steps 3 through 7. Start with the values you know: the number of sides, the number of dice and the modifier. Use let for each.
Hint 2
The average of one die is the sum of its lowest and highest faces divided by two. For a d8, the lowest face is 1 and the highest is 8. Remember that Int division can truncate, so make sure at least one side of the division is a Double. Store the result with a Double type annotation, as in let averagePerDie: Double = ....
Hint 3
Multiply the number of dice by the average. Because the number of dice is an Int, convert it with Double(...) before multiplying. Then add the modifier, converted to a Double in the same way. Finish with one print call whose string contains interpolated values for the dice count, the sides, the modifier and the total.
Extra practice
Exercise 1 — Predict the output
Without running anything, predict the output of this playground. Then run it and compare.
#Playground("Predict") {
var score = 10
score += 5
score = score - 3
let bonus = 2
print("Score: \(score + bonus)")
}
Questions:
- What is
scoreafter the second line? - What is
scoreafter the third line? - What text does
printwrite?
Exercise 2 — Spot the bug
A student wrote this playground. It does not build. Find the error without running it, and explain it in one sentence.
#Playground("Broken average") {
let rolls = 4
let total: Double = 14
let average = total / rolls
print(average)
}
Then rewrite the line that causes the error so that it builds and prints 3.5.
Exercise 3 — Trace the optional
A playground contains this code. Predict what it prints for each of the three inputs: "7", "seven" and "" (an empty string). Explain your answer for each input.
let input = "seven"
let parsed = Int(input)
let value = parsed ?? -1
print(value)
Then change the input value to each of the three strings and run the playground to check your predictions.
Exercise 4 — Scope
In one playground, declare let secret = 42. In a second playground, try to print secret. Predict the error message, then run it. Write one sentence explaining why the error happens, in terms of braces and scope.
Exercise 5 — Read a comment
Here is a line from the reference file:
let number = Int(typedText) // Int? because the text might not be a number
Rewrite the comment so that it explains the decision in fewer words, and explain why the new comment is still useful to a reader.