Learn Courses My Dashboard

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)
        
        return totalWithTax/numPeople
    }
    
}

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 "

1 Like

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)