-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates to 1) add guard, 2) use .isMultiple(of:) and 3) use switch st…
…atement
- Loading branch information
1 parent
71431b1
commit f94bccc
Showing
2 changed files
with
19 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters