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

matt tripodi
matt tripodi
2,673 Points

Help with challenge

can someone please help me with the challenge after this lesson for some reason I cannot get it right

5 Answers

Stage 1 is asking you to create a method called calculateTaxes that takes a parameter of type Double called percentage.

Try:

func calculateTaxes(percentage: Double){ }

That stipulates that calculateTaxes is a method or function, names it and names the incoming parameter and states of what type it is.

The next stage asks you to add a Double as the return type. That's the arrow thing:

func calculateTaxes(percentage: Double) -> Double { }

That stage also wants you to return an expression provided:

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

Next we're asked to create an instance of Expense called item and use the init method with whatever we want to call it. I chose "Hotel".

var item = Expense(description: "Hotel")

We're also asked to assign the amount property a value of 100.

item.amount = 100

Next up, we want to create a new variable called taxes and use the method we created to assign a value to it:

var taxes = item.calculateTaxes(7.5)

I hope that made sense!

Steve.

Hi Jack,

Picking up on one of your points there; the multiplication issue isn't quite as you think.

The method takes the percentage of taxes as a parameter and divides that by 100 within the method.

    func calculateTaxes(percentage: Double) -> Double {
      return (self.amount * (percentage/100))  // this converts the number passed in
    }

Is this what you mean by they never said during the course that they never said that you can multiply by using pxrathethese.? The parentheses aren't doing the multiplication - they are receiving the parameter for the above method/function. The maths is done inside the method, not due to the brackets.

I hope this helps, but please give me a shout if it doesn't. I'd not want you to carry on thinking TTH are anything other than trying to deliver solutions for you.

Shout me on here, or on Twitter, I'm @OnlySteveH.

Steve.

Sure - how far have you got with it?

Jarrett Young
Jarrett Young
12,635 Points

I made it to task 4 of 4 but it won't let me pass, what am I doing wrong?

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


    init (description: String) {
        self.description = description
        item.amount = 100
    }

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

}

var item = Expense(description: "Hotel")

var taxes = item.calculateTaxes(7.5)

Hi Jarrett,

You've put the item.amount = 100 inside the struct. The instance of the struct called item exists only outside of its own definition. Move that line to after the line where you declare the instance called item.

So,

var item = Expense(description: "Hotel")
// use the new instance to set the 'amount' variable
item.amount = 100

var taxes = item.calculateTaxes(7.5)

You can't initialise an instance inside the initialiser! I hope that makes sense. And I hope it works too!!

Steve.

I hope they realize that they never said during the course that they never said that you can multiply by using pxrathethese. I am aware that you can, but having the challenge being so picky that it only accepts that isn't right. Also, if they wanted to find the taxes on that 100 dollars, they should by multiplying by .075 not 7.5. Multiplying by 7.5 gives you 700+ dollars in taxes, but multiplying by .075 states that you owe 7.5 dollars in taxes which is what they actually owe. Please team tree house, fix these issues.

Also, the challenge as a whole is much to vague. Please edit the instructions to make them more clear to the us. I had to resort to just copying answers from this help page because I couldn't understand the end goal of the challenge.