I have two similar solutions for the Chapter 5 Silver Challenge but I’m wondering if there is a better solution. My solutions are as follows:
if case 18...35 = age, age >= 21, age < 30 || age > 39 {
print("In cool demographic and of drinking age and not in their thirties")
}
if case 18...35 = age, age >= 21, !(age >= 30 && age <= 39) {
print("In cool demographic and of drinking age and not in their thirties")
}
I’m not entirely happy with those solutions. Is there a more “Swift” solution?
I was trying to do this one a bit different… anyone have any ideas on how I could make this work?
Kinda trying to see if I can condense the switch conditions into one line but still populate the print() associated to the correct condition.
The error I’m getting is asking for me to init() it.
“Variable binding in a condition requires an initializer.”
Anyone have any ideas?
var age = 21
var age1 = 26…30
var age2 = 30…120
if case 18…120, age == 21,(age1 = 22…29), age2 > 30 {
print("(age) is in the cool demographic")
print("(age1) is in the really cool demographic")
print("(age2) is in the big time cool demographic")
}
if case 18…35 = age, age >= 21, age < 30 || age > 39 {
print(“In cool demographic and of drinking age and not in their thirties”)
}
this is my solution
if case 18...35 = age, age > 21, age < 30
{
print("cool drinking age")
}
Now this was wrong as my solution does not specifically take care to take out only those who are in their 30s, ie from 30 to 39, so if i were to change age to 40 and code as below
let age = 40
if case 18...35 = age, age > 21, age < 30
{
print("cool drinking age")
}
The code does not give any print out…
So my updated solution is as below
if case 18...50 = age, age >= 21, !(age >= 30 && age <= 39)
{
print("cool age")
}
here i use ! logical operator to strike out age group of 30s ie from 30 to 39 and also check for age being > 21