Silver Challenge Solution?

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?

Thanks,
–Don

I think your first solution is fine already without " || age > 39". The age will not be more than 35 because of the first condition, the 18…35 range.

Cheers,
Raf

This is the code I came up with:

let age = 22

if case 18…35 = age, age >= 21, age <= 30
{
print(“In cool demographic, of drinking age and under 30”)
}

Another way to do this using an if statement is:

if 18…35 ~= age, age >= 21, age <= 30
{
print(“In cool demographic, of drinking age and under 30”)
}

Works the same as if-case.

Mitch

For fun, here is my solution:

if case 18...35 = age, age >= 21 , age/10 != 3 {print ("text...")}

This is what I went with:

if case 18...35 = age, age >= 21, age < 30 {
    print("In cool demographic, of drinking age, and not in thirties.")
}

Since the “cool demographic” doesn’t extend past age 35, I check that the age is less than 30. I didn’t see the need to test the entire 30-39 range.

The first 2 examples here fail when age = 30 thru 35, as these aren’t under 30.

The third example fails when age = 30, as this isn’t under 30.

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

Myccpay

Hello @rusev,

This was my earlier solution -

let age = 25

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

Thanks
Amit Shrivastava

You are so right! Thank you for pointing it out.

1 Like

I realize the book wants to add another condition, but it’s not really necessary. Just change the range to be under 30.

This was my condensed solution:
if case 18…29 = age, age >=21 {
print(In cool demographic, of drinking age, and not in their 30s)
}