Skip to content

Commit ea8c268

Browse files
committed
Tuples - Initial movie review playgrounds
1 parent c268100 commit ea8c268

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
import Foundation
3+
4+
func normalisedStarRating(forRating rating: Float,
5+
ofPossibleTotal total: Float) -> (Int, String) {
6+
7+
let fraction = rating / total
8+
let ratingOutOf5 = fraction * 5
9+
let roundedRating = round(ratingOutOf5) // Rounds to the nearest integer.
10+
let numberOfStars = Int(roundedRating) // Turns a Float into an Int
11+
let ratingString = "\(numberOfStars) Star Movie"
12+
return (numberOfStars, ratingString)
13+
}
14+
15+
let ratingValueAndDisplayString = normalisedStarRating(forRating: 5, ofPossibleTotal: 10)
16+
17+
let ratingValue = ratingValueAndDisplayString.0
18+
print(ratingValue) // 3 - Use to show the right number of stars
19+
20+
let ratingString = ratingValueAndDisplayString.1
21+
print(ratingString) // "3 Star Movie" - Use to put in the label
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
import Foundation
3+
4+
func normalisedStarRating(forRating rating: Float,
5+
ofPossibleTotal total: Float)
6+
-> (starRating: Int, displayString: String) {
7+
8+
let fraction = rating / total
9+
let ratingOutOf5 = fraction * 5
10+
let roundedRating = round(ratingOutOf5) // Rounds to the nearest integer.
11+
let numberOfStars = Int(roundedRating) // Turns a Float into an Int
12+
let ratingString = "\(numberOfStars) Star Movie"
13+
return (starRating: numberOfStars, displayString: ratingString)
14+
}
15+
16+
let ratingValueAndDisplayString = normalisedStarRating(forRating: 5, ofPossibleTotal: 10)
17+
18+
let ratingValue = ratingValueAndDisplayString.starRating
19+
print(ratingValue) // 3 - Use to show the right number of stars
20+
21+
let ratingString = ratingValueAndDisplayString.displayString
22+
print(ratingString) // "3 Star Movie" - Use to put in the label
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)