Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Enums and Structs Structs and their Methods Struct Methods

Struct Method - Swift Enums and Structs

Hey all. I'm having this issue with the challenge on Swift Enums and Structs course. My code is compiling with no errors but I'm still getting an error when submitting the answer. Thanks in advance!

Add a method to the Expense struct that will calculate taxes based on a percentage passed to the method. In this task, add a method named calculateTaxes which accepts a parameter named 'percentage' of type Double and does not return anything. (In the next task we will add the calculations to the method).

Add a Double as a return type to the calculateTaxes method and within the method return the following expression: (self.amount * (percentage/100)).

struct.swift
struct Expense {
    var description: String
    var amount: Double = 0.0
    var percentage: Double

    init (description: String, percentage: Double) {
        self.description = description
        self.percentage = percentage
    }

    func calculateTaxes(percentage: Double) -> Double {
    return (self.amount * (percentage/100))

 }
}

1 Answer

Chris Shaw
Chris Shaw
26,650 Points

Hi Tony,

You have a minor error in your code which is you've specified a new description property in the struct which isn't required as description is a parameter so it's already assigned as a contant within the method.

The final code for task 2 you should have is as follows.

struct Expense {
  var description: String
  var amount: Double = 0.0

  init (description: String) {
    self.description = description
  }

  func calculateTaxes(percentage: Double) -> Double {
    return (self.amount * (percentage/100))
  }
}

Happy coding!

Thank you!