Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active July 16, 2024 10:57
Show Gist options
  • Save TheMuellenator/569463645ec81ea49f85ebf6d6dd99fe to your computer and use it in GitHub Desktop.
Save TheMuellenator/569463645ec81ea49f85ebf6d6dd99fe to your computer and use it in GitHub Desktop.
iOS repl.it - Structures Challenge Solution
// Define a struct
struct User {
var name: String
var email: String?
var followers: Int
var isActive: Bool
func logStatus() {
if (isActive) {
print("\(name) is working hard")
} else {
print("\(name) has left earth")
}
}
}
// Initialise the struct
var branson = User(name: "Richard", email: nil, followers: 0, isActive: false)
branson.logStatus()
// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", followers: 2001, isActive: true)
musk.logStatus()
print("Contacting \(musk.name) on \(musk.email!) ...")
print("\(musk.name) has \(musk.followers) followers")
// sometime later
musk.isActive = false
musk.logStatus()
@bnbrwd
Copy link

bnbrwd commented Nov 13, 2021

this is my solution

func exercise() {

// Define the User struct here

struct User{
var name : String
var email : String?
var followers : Int
var isActive : Bool

    init(name: String, email: String?, followers: Int, isActive: Bool){
        self.name = name
        self.email = email
        self.followers = followers
        self.isActive = isActive
    }
    
    func logStatus(){
        if (isActive) {
            print("\(name) is working hard")
        }else{
            print("\(name) has left earth")
        }
        
    }
}

// Initialise a User struct here

    let userObj = User(name: "Richard", email: nil, followers: 0, isActive: false)
    userObj.logStatus()



// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", followers: 2001, isActive: true)
musk.logStatus()
print("Contacting \(musk.name) on \(musk.email!) ...")
print("\(musk.name) has \(musk.followers) followers")
// sometime later
// musk.name = "Richard"
musk.isActive = false
musk.logStatus()

}

@DozeStrawberry
Copy link

func exercise() {

// Define the User struct here
struct User{
    var name: String
    var email: String?
    var followers: Int
    var isActive: Bool
    
    init(name: String, email: String?, followers: Int, isActive: Bool){
        self.name = name
        self.email = email
        self.followers = followers
        self.isActive = isActive
    }
    
    func logStatus(){
        if isActive == true{
            print("\(name) is working hard")
        }else{
            print("\(name) has left earth")
        }
    }
}

// Initialise a User struct here
let message = User(name: "Sun", email: "Hiyou@gmail.con", followers: 150, isActive: false)
message.logStatus()


// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", followers: 2001, isActive: true)
musk.logStatus()
print("Contacting \(musk.name) on \(musk.email!) ...")
print("\(musk.name) has \(musk.followers) followers")
// sometime later
musk.isActive = false
musk.logStatus()

}

exercise()

@Stapa1
Copy link

Stapa1 commented Jun 14, 2022

func exercise() {

// Define the User struct here
struct User{
    let name: String
    let email: String?
    var followers: Int
    var isActive: Bool

func logStatus(){
if (isActive == true) {
print("(name) is working hard")
}else{
print("(name) has left earth")
}
}

}

// Initialise a User struct here
var Richard = User(name: "Richard", email: "Richard@yahoo,com", followers: 0, isActive: false)

Richard.logStatus()

// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", followers: 2001, isActive: true)
musk.logStatus()
print("Contacting \(musk.name) on \(musk.email!) ...")
print("\(musk.name) has \(musk.followers) followers")
// sometime later
musk.isActive = false
musk.logStatus()

}

@NavakanthMesala
Copy link

func exercise() {

// Define the User struct here
struct User {
    
   var name: String 
   var email: String? 
   var followers: Int 
   var isActive: Bool 
   
   func logStatus(){
       if(isActive){
           print("\(name) is working hard" )
       }
       else{
           print("\(name) has left earth" )
       }
   }
   
   init(name:String, email: String?, followers: Int, isActive: Bool){
       self.name = name
       self.email = email 
       self.followers = followers
       self.isActive = isActive
   }
    
    
}


// Initialise a User struct here

var info = User(name: "Richard", email: nil, followers: 0, isActive: false)

info.logStatus()

// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", followers: 2001, isActive: true)
musk.logStatus()
print("Contacting \(musk.name) on \(musk.email!) ...")
print("\(musk.name) has \(musk.followers) followers")
// sometime later
musk.isActive = false
musk.logStatus()

}

@alexnavter
Copy link

alexnavter commented Oct 19, 2023

// Define a structure called User with its properties
struct User {
    let name: String
    var email: String?
    var followers: Int
    var isActive: Bool

// Initializer method for the User struct. It will allow us to create a User object.
    init(name: String, email: String? = nil, followers: Int, isActive: Bool) {
        self.name = name
        self.email = email
        self.followers = followers
        self.isActive = isActive
    }

// Method defined within the User struct to check if the isActive property is true or false to print the user's status to the console
    func logStatus(){
        
        if(isActive){
            print("\(name) is working hard")
        } else {
            print("\(name) has left earth")
        }
    }
}

// Create an instance of the User struct and initialize it with its values
var richard = User(name: "Richard", email: "richard@yahoo.com", followers: 0, isActive: false)

richard.logStatus() // Output: Richard has left earth

@BunyaminMete
Copy link

func exercise() {

// Define the User struct here
struct User {
    var name : String
    var email : String?
    var followers : Int
    var isActive : Bool
    
    func logStatus(){
        if self.isActive == true {
            print("\(self.name) is working hard")
        }
        else {
            print("\(self.name) has left earth")
        }
    }
}



// Initialise a User struct here
 var userRichard = User(name: "Richard",email: nil, followers: 0, isActive: false)
 userRichard.logStatus()



// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", followers: 2001, isActive: true)
musk.logStatus()
print("Contacting \(musk.name) on \(musk.email!) ...")
print("\(musk.name) has \(musk.followers) followers")
// sometime later
musk.isActive = false
musk.logStatus()

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment