Help me with using of computed properties in the Playground

We're created a struct and a function that compares sizes of two rectangles:

struct Rectangle {

    let width: Int

    let height: Int

    

    func isBiggerThan(_ rectangle: Rectangle) -> Bool {

        let areaOne = width * height

        let areaTwo = rectangle.width * rectangle.height

        return areaOne > areaTwo

    }

}
let rectangle = Rectangle(width: 10, height: 10)

let otherRectangle = Rectangle(width: 10, height: 20)



rectangle.isBiggerThan(otherRectangle)

otherRectangle.isBiggerThan(rectangle)

My exercise is: Simplify the isBiggerThan method by creating a computed property named area for the rectangle struct and then using the computed property inside the isBiggerThan() method.

Could someone please explain how the previous code works and how to simplify this code by using a computed property

Answered by Claude31 in 709146022

You can do this:

struct Rectangle {
    let width: Int
    let height: Int

   var area : Int {
       return width * height
   }
    
    func isBiggerThan(_ rectangle: Rectangle) -> Bool {
        return self.area > rectangle.area
    }

}
Accepted Answer

You can do this:

struct Rectangle {
    let width: Int
    let height: Int

   var area : Int {
       return width * height
   }
    
    func isBiggerThan(_ rectangle: Rectangle) -> Bool {
        return self.area > rectangle.area
    }

}
Help me with using of computed properties in the Playground
 
 
Q