Search code examples
swiftswift3

How to solve: "activityLevel be an Int that will represent a scoring 1-10 of how active they are. Implement this now."


HI guys I try to solve this exercise: For most apps you'll need to have a data structure to hold information about a user. Create a User struct that has properties for basic information about a user. At a minimum, it should have properties to represent a user's name, age, height, weight, and activity level. You could do this by having name be a String, age be an Int, height and weight be of type Double, and activityLevel be an Int that will represent a scoring 1-10 of how active they are. Implement this now.

but I don't understand when he says: activityLevel be an Int that will represent a scoring 1-10 of how active they are. Implement this now.

I don't know if it's right.

below post my code:

struct User {
    let name : String
    var age : Int
    var height : Double
    var weight : Double
    var activityLevel = [1,2,3,4,5,6,7,8,9,10]
}

Solution

  • I think they are saying that your activityLevel be an Int, that can only be the values 1-10:

    struct User {
        let name : String
        var age : Int
        var height : Double
        var weight : Double
        var activityLevel : Int
    }
    

    And then when you try and set the value, you would need to enforce that it is in the correct range:

    init?(name: String, age: Int, height: Double, weight: Double, activityLevel: Int){
        guard activityLevel > 0 && activityLevel < 11 else { return nil }
    
        self.name = name
        self.age = age
        self.height = height
        self.weight = weight
        self.activityLevel = activityLevel
    }