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 trialAry de Oliveira
28,298 PointsSwift my code no work need help ....
Challenge Task 2 of 2
In this game of ours, we have an odd scoring mechanism . At the end of the round, if your score is 10, you lose! If it's anything but 10, you win.
Declare a variable named isWinner and assign the results of a comparison operation to check whether the player has won or not. If the total score is not 10, then the player has won, otherwise he or she has lost. (Hint: Use the NOT operator)
or 10 != 10
// Enter your code below
var initialScore = 8
let totalScore = ++initialScore
var isWinner = 10 != 10
4 Answers
Steve Hunter
57,712 PointsHi there,
You're nearly there.
var isWinner = totalScore != 10
first, declare the variable isWinner
then assign the result of comparing totalScore != 10
.
A bit of an odd solution, but there it is!
Steve.
Ary de Oliveira
28,298 Pointsthanks Steve...
Steve Hunter
57,712 PointsNo problem!
Djalma Barbieri
2,331 PointsNo erros on the editor, but on Recheck work it is ! (Swift enums and structs, challenge task 3)
struct Expense { var description: String var amount: Double = 0.0 var item: String = "Item"
init (description: String, amount: Double, item: String) {
self.description = description
self.amount = 100
self.item = item
}
func calculateTaxes(percentage: Double) -> Double {
return (self.amount * (percentage/100))
}
}
Steve Hunter
57,712 PointsI'm not at my computer right now, but I don't think you want item either in the init method signature or in the init method body.
And self.amount wants to equal amount, not a number, I think.
Remove mention of item and see if that works. Let me know if it doesn't and I'll get a solution to you. That won't be today, unfortunately, as it's late now. But I'll get in to it tomorrow.
Djalma Barbieri
2,331 PointsHi Steve, I change the program, but it doesn't work, I don't know where's the problem.
Steve Hunter
57,712 PointsHi there,
Right, I found the challenge and am at my computer.
There's no need to amend the init
method at all. You first implement the method calculateTaxes
which you have done just fine.
Then, the question asks you to declare a variable called item
and assign an instance of Expense
to it, using the constructor as defined in the question. You do that outside of the struct
as you are using that template to create an instance. You then access that new instance, item
, and assign a value to its amount
property. Your code should end up looking like:
struct Expense {
var description: String
var amount: Double = 0.0
init (description: String) {
self.description = description
}
// add the calculateTaxes method here
// it should accept only one parameter named 'percentage' of type Double
func calculateTaxes(percentage: Double) -> Double {
return (self.amount * (percentage/100))
}
}
var item = Expense(description: "Hotel")
item.amount = 100
I hope that helps.
Steve.
Djalma Barbieri
2,331 PointsThanks a lot Steve. I did not understand the statement , now with his explanation was very clear !
Steve Hunter
57,712 PointsThank you - glad it helped.
Steve.