-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathContents.swift
38 lines (29 loc) · 890 Bytes
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import UIKit
// Exercise - Functions and Optionals
func checkAge (age: String) -> Int?{
if let userAge = Int(age) {
if (userAge > 18) {
print("Welcome!")
} else {
print("Sorry, but you aren't old enough to use our app.")
}
return userAge
} else {
print("Sorry, something went wrong. Can you please re-enter your age?")
}
return nil
}
let userInputAge: String = "34e"
checkAge(age: userInputAge)
checkAge(age: "23")
print(checkAge(age: "17"))
// ------
var prices = ["Chips": 2.99, "Donuts": 1.89, "Juice": 3.99, "Apple": 0.50, "Banana": 0.25, "Broccoli": 0.99]
var stock = ["Chips": 4, "Donuts": 0, "Juice": 12, "Apple": 6, "Banana": 6, "Broccoli": 3]
func getCost(item: String) -> Double? {
if (stock[item]! > 0) {
return prices[item]
}
return nil
}
print(getCost(item: "Chips"))