Dicoding-Day 6
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
func greet(person: [String: String]) {
guard let name = person["name"] else {
return
}
print("Hello \(name)!")
guard let location = person["location"] else {
print("I hope the weather is nice near you.")
return
}
print("I hope the weather is nice in \(location).")
}
greet(person: ["name": "John"])
greet(person: ["name": "Vincent", "location": "London"])
print("\n==============================================================")
print("Latihan Logika Penghitung Nilai!")
print("Selamat datang di Dicoding Academy!")
print("Input banyak nilai yang akan dihitung", terminator: ": "); let total = readLine() ?? ""
print("\n==============================================================")
if let totalItem: Int = Int(total) {
var allValues = [Int]()
for index in 1...totalItem {
print("Masukkanlah item ke \(index)", terminator: ": "); let newItem = Int(readLine() ?? "") ?? 0
allValues.append(newItem)
}
print("\n==============================================================")
var sum: Int = 0
for (_, value) in allValues.enumerated() {
sum+=value
}
let average = sum/totalItem
var grade = ""
switch average {
case 0...40:
grade = "E";
break
case 41...50:
grade = "D";
break
case 51...60:
grade = "C";
break
case 61...70:
grade = "C+";
break
case 71...80:
grade = "B";
break
case 81...90:
grade = "B+";
break
case 91...100:
grade = "A";
break
default:
grade = "Invalid!";
}
if grade != "Invalid!" {
print("Anda mendapatkan grade nilai: \(grade).")
print("Anda mendapatkan total nilai: \(sum), dengan rata-rata: \(average)")
} else {
print("Nilai Anda Invalid!")
}
} else {
print("Nilai Anda Invalid!")
}
print("\n==============================================================")
Swift
INFO