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

Keep it simple.

Write it like this (however yucky):

if case 21...35 = age
{
    print ("In cool demographic, of drinking age and under 30")
}

or like this:

switch age
{
    case 21...35:
        print ("In cool demographic, of drinking age and under 30")
    default: break
}

or even better like this:

if age >= 21 && age <= 30
{
    print ("In cool demographic, of drinking age and under 30")
}

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")
}