Solution to Lesson 9 Challenge gives errors

The solution for Challenge 9 gives error “Cannot convert value of type ‘Int’ to expected argument type ‘Double’”

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

Wrap your numPeople with Double() like this:

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

Hello, I am expirencing the same issue as nadinenel.

When I wrap numPeople with Double(), I received a new error "Cannot convert value of type’(Double) → Double’ to expected argument type “Double”

My Code: *** shows where error message is given***
struct TaxCalculator {
var tax = 0.15
func totalWithTax(billtotal:Double) → Double {
return billtotal * (1 + tax)
}
}

struct BillSplitter {

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

    let taxCal = TaxCalculator()
    let totalWithTax = taxCal.totalWithTax(billtotal: )
    
    return totalWithTax / Double(numPeople) ***this is where the error message populates***
}

}
let split = BillSplitter()
print(split.splitBy(billtotal: 120, numPeople: 5))

Disregard, found solution. Changed: let totalWithTax = taxCal.totalWithTax(billtotal: )
to: let totalWithTax = taxCal.totalWithTax(billtotal: billtotal)

    Works fine now.

Thank you so much for sharing the solution! That is awesome. I had changed it by putting Double instead of Int but knew that was not a good solution because the number of people should only be able to be a whole number. However, the solution in DropBox should be fixed as well. I would not have thought of what you did since I am brand new to this.

1 Like

A post was split to a new topic: Help regarding Lesson 9 Tax Challenge