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 trialdakota ferrell
2,336 Pointsif statement in functions
How do if statements work in functions I was messing around with this code and it will not run I dont know what is wrong? Is it because I cant use if statements on arguments inside of a function or is it for a different reason?
func greet (Name: String, Day: String) {
let greetings = "hi " + Name + "today is " + Day
if Day = Firday {
print("Party time")
}
print(greetings)
}
greet(Name: "Dakota", Day: "Friday")
1 Answer
Steve Hunter
57,712 PointsHi there,
To test for equality, use double equals ==
and if you want to see if a variable is equal to a string, use ""
around the string.
if Day = Firday
becomes
if Day == "Friday"
Lastly, you probably shouldn't use capital initials for Day
or Name
but that's semantics.
Steve.
dakota ferrell
2,336 Pointsdakota ferrell
2,336 PointsThanks but how would I get Party time to print to the screen because it will not print to the bottom consol
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsDoes the greeting print out?
Can you posts your amended code and I'll have a look.
Steve.
dakota ferrell
2,336 Pointsdakota ferrell
2,336 PointsYeah greetings prints out but party time does not
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsTypo - you are comparing
firday
tofriday
- correct one of them!