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

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,575 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)