Skip to content

Commit

Permalink
Updates to 1) add guard, 2) use .isMultiple(of:) and 3) use switch st…
Browse files Browse the repository at this point in the history
…atement
  • Loading branch information
l-rettberg committed May 9, 2020
1 parent 71431b1 commit f94bccc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
34 changes: 17 additions & 17 deletions Fizz Buzz/FizzBuzz.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// last checked with Xcode 10.0 (10A255)
// Last checked with Xcode Version 11.4.1 (11E503a)

func fizzBuzz(_ numberOfTurns: Int) {
for i in 1...numberOfTurns {
var result = ""

if i % 3 == 0 {
result += "Fizz"
guard numberOfTurns >= 1 else {
print("Number of turns must be >= 1")
return
}

if i % 5 == 0 {
result += (result.isEmpty ? "" : " ") + "Buzz"

for i in 1...numberOfTurns {
switch (i.isMultiple(of: 3), i.isMultiple(of: 5)) {
case (false, false):
print("\(i)")
case (true, false):
print("Fizz")
case (false, true):
print("Buzz")
case (true, true):
print("Fizz Buzz")
}
}

if result.isEmpty {
result += "\(i)"
}

print(result)
}
}

fizzBuzz(100)
fizzBuzz(15)
4 changes: 2 additions & 2 deletions Fizz Buzz/FizzBuzz.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Updated for Xcode Version 11.4.1 (11E503a)
// Last checked with Xcode Version 11.4.1 (11E503a)

func fizzBuzz(_ numberOfTurns: Int) {
guard numberOfTurns >= 1 else {
Expand All @@ -18,4 +18,4 @@ func fizzBuzz(_ numberOfTurns: Int) {
print("Fizz Buzz")
}
}
}
}

0 comments on commit f94bccc

Please sign in to comment.