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

Aleksi Tappura
Aleksi Tappura
10,286 Points

Structs and their Methods: Challenge Task 3 of 4

Hey so I can't get any idea what to do in this part:

Create a variable named item and assign it an instance of Expense (remember to use the initializer with the description parameter. Enter any description you like). On the next line assign the amount property a value of 100.

I tried tons of stuff but any of those doesn't seem to work. Here's my current code that I made in 2 of 4:

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

I would really appreciate if someone would help me a little!

3 Answers

Laurence Bowe
Laurence Bowe
6,222 Points

Hey, Hopefully the code below will help! Don't worry everyone struggles! Here is the code:

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) } // it should accept only one parameter named 'percentage' of type Double

}

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

Hope I helped! :) (:

PS: for anyone who still have difficulties to solve it. Just to point out that the value needs to be a "Double 0.0" and a not "Integer 0"

don't forget it.

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

Abdoulaye Diallo
Abdoulaye Diallo
15,863 Points

struct Expense { var description: String var amount: Double = 0.0 init (description: String) { self.description = description } // add the calculateTaxes method here // it should accept only one parameter named 'percentage' of type Double. func calculateTaxes ( percentage: Double)->Double{ return ( self.amount * ( percentage / 100 ) ) } }

//Declare the variable outside the Struct. var item = Expense(description: "item variable") item.amount = 100.0