Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Enums and Structs Structs and their Methods Struct Methods

Edwin Mhoy Silva Rifa
Edwin Mhoy Silva Rifa
2,631 Points

We need to figure out the tax amount when the tax percentage is 7.5 percent. Call the method calculateTaxes on the varia

Help, i don´t know what I´m been asked to do?

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

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

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

}
var  item = Expense(description:"kage")
var 
calculateTaxes()
Jan Gooßens
Jan Gooßens
22,344 Points

Some time ago i did the same excercise... if i remember correctly it was something like that :

var result = item.calculateTaxes(7.5)

You have to store the result of item with 7.5 % taxes in a variable (in this case "result")

we call the function calculateTaxes on item, It waits for a Double (7.5)

hope i can help you. Im new to swift so still learning, too :)

Gareth Gomersall
Gareth Gomersall
2,890 Points

Edwin Rifa the code should be like Jan Goo said above

i.e.

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

var item = Expense(description: "Travel")
item.amount = 100

var taxes = item.calculateTaxes(7.5)

1 Answer

Try

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

var item = Expense(description: "item")
item.amount = 100.0

var taxes = item.calculateTaxes(7.5)