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

Mitchell Parrish
Mitchell Parrish
1,092 Points

Swift Enums and Struc Help Challenge 4 or 4

This is how far i have gotten. X-Code playground shows Double -> Double but the treehouse says its not returning a expected Double? I'm lost!!!

struct.swift
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.init(description: "I hope this works")
item.amount = 100
var taxes = Expense.calculateTaxes(item)

2 Answers

Jhoan Arango
Jhoan Arango
14,547 Points

Hello:

You are doing great, but the challenge is asking to call the method calculate taxes, to figure out how much in taxes item will be.

Since calculate taxes has a parameter of type double, and the challenge ask you to calculate 7.5 on item, then you would call it like this.

item.calculateTaxes(7.5)

Then the method will calculate taxes on the 100.

Your code should look like this

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:"Car")
item.amount = 100
var taxes = item.calculateTaxes(7.5)

Good luck, hope this helps

Mitchell Parrish
Mitchell Parrish
1,092 Points

Ah. I see what you mean. I tried every combination but that one... Go Figure! Thank you for the help and explanation!

Shandas Duchintav
Shandas Duchintav
8,222 Points

I solved mine this way and I completed the challenge.

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

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

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

  }

}

var item = Expense.init(description: "Ticket")
item.amount = 100
var taxes = item.calculateTaxes(7.5)