# 14 Day Beginner Challenge (SwiftUI) - Lesson 9 challenge

After completing lesson 9 fairly confidently, I was confused as to why I still had an error. I checked Chris’s answer key and saw that his code was the same as mine, other than variable names. I even copy and pasted Chris’s code into my playground but still had the error. For the lesson 9 challenge, the number of people variable is supposed to be an integer - however, Xcode does not allow a Double to be divided by an integer. I am aware of type casting from Java but can’t seem to understand if there is still something I’m missing or if I should simply change the numPeople variable into a Double for this challenge?

2 Likes
1 Like

The solution should be fixed soon. It should say:

``````return totalWithTax / Double(numPeople)
``````

Also be careful when naming your variables. Shortening them by saying `totalWTax` might make sense to you now but when you come back to your code months later (or someone else reads your code), it may not be clear.

3 Likes

This solution still isn’t fixed in the files that I downloaded maybe a week ago.
The provided solution is the following:

``````struct TaxCalculator {

var tax = 0.15

func totalWithTax(_ subtotal:Double) -> Double {
return subtotal * (1 + tax)
}
}

struct BillSplitter {

func splitBy(subtotal:Double, numPeople:Int) -> Double {

let taxCalc = TaxCalculator()
let totalWithTax = taxCalc.totalWithTax(subtotal)

}

}

let split = BillSplitter()
print(split.splitBy(subtotal: 120, numPeople: 5))
``````

The correct solution should have something like this for line 17

``````        return totalWithTax/Double(numPeople)
``````

It’s not too big of a deal because Xcode does throw the below error and will even fix it for you but I can see that it might be confusing to people who don’t remember the part where he said math has to be done on the same data types and people don’t know what casting to another type is

I’m sure the reason the challenge explicitly says to make sure numPeople is an int is to create this error and cause the student to need to google or understand the error but the solution being wrong might contribute to confusion or someone thinking there might have been a change to Xcode since the course was released

2 Likes

Hey is someone able to explain this part:

let totalWithTax = taxCalc.totalWithTax(subtotal)

why do we need the taxCalc. Im not clear on the bulletpoint then totalWithTax
Does it mean to reference the other method?

Thanks!

1 Like

`taxCalc` has been declared as an instance of the struct TaxCalculator.

ie: above that you should see:

``````var taxCalc = TaxCalculator()
``````

We can now use `taxCalc` to allow us to access the function inside the struct `TaxCalculator` by using what is referred to as dot notation. ie:

``````let totalAmountIncludingTax = taxCalc.totalWithTax(subtotal)
``````

is saying, "Using the function `totalWithTax(subtotal)` and passing in the value `subtotal`, assign the returned value to `totalAmountIncludingTax `"

2 Likes

Still not fixed. But Xcode points to the solution.

toot a while to figure out the conversion of the Int to the Double for the math.

Yes, I suspect that Chris Ching deliberately presented the Challenge in that way so that you would discover that the data type didn’t match and it would present an error. What that does is reinforce that any math calculations must have the same data type.

The thread subject title has been clarified so that it says that the Lesson 9 challenge is from the 14 Day Beginner Challenge (SwiftUI)

I hope it is right

@reema

Not quite. Check your TaxCalculator struct. You are returning only 1 + the tax rate which equates to 1.15. You have not included the passed in parameter `total` in your calculation.

Look at the value in the right hand panel in your playground on the same row as

``````func totalWithTax(_total: Double) -> Double {
``````

Using your code in my Playground, that shows as 1.15

Also note that if you intend to suppress the parameter name in your function you must have a space between the underscore `_` and the parameter name. ie:

``````func totalWithTax(_ total: Double) -> Double {
``````

This will mean that when you call that function you only require subtotal to be entered. ie your code would look like this:

``````struct TaxCalculator {

var tax = 0.15

func totalWithTax(_ total: Double) -> Double {
return 1 + tax  // You need to include the parameter 'total' in your calculation
}
}

struct BillSplitter {

func splitBy(subtotal:Double, numPeople:Int) -> Double {
let taxCalc = TaxCalculator()
let totalWithTax = taxCalc.totalWithTax(subtotal)
}
}
``````

If you do not make use of the underscore to suppress that parameter then your code would look like this:

``````struct TaxCalculator {

var tax = 0.15

func totalWithTax(total: Double) -> Double {
}
}

struct BillSplitter {

func splitBy(subtotal:Double, numPeople:Int) -> Double {
let taxCalc = TaxCalculator()
let totalWithTax = taxCalc.totalWithTax(total: subtotal)
}
}
``````

@Chris_Parker thanks,
but if you don’t mind, my Xcode cannot run it
could you check it? what’s will return. see

Works for me.

``````struct TaxCalculator {

var tax = 0.15

func totalWithTax(subtotal: Double) -> Double {
return subtotal * (1 + tax)
}
}

struct BillSplitter {

func splitBy(subtotal:Double, numPeople:Int) -> Double {
let bill = TaxCalculator()
return bill.totalWithTax(subtotal: subtotal) / Double(numPeople)
}
}

let split = BillSplitter()
print(split.splitBy(subtotal: 600, numPeople: 6))
``````

The output is 115.0

1 Like

thanks!

Sigh… I was trucking along, came to this lesson, and hit a wall. My brain is getting scattered a little.