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 trialRahul Wadhavkar
1,080 Pointshi..I don't get this challenge, can't paste. I don't understand the purpose of the code after the If let stmt..help pls?
func search(#name: String) -> String? {
let names = ["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey"]
for n in names {
if n == name {
return n
}
}
return "nil"
if let x = search (name: "Snow White") {
println("Name Found: \(x)")
}
}
1 Answer
Chris Shaw
26,676 PointsHi Rahul,
You have a number of problems with your code, see the below.
-
"nil"
shouldn't be wrapped in quotes as it's a type, it should just benil
- You have your
IF
statement inside your function, it should be after the closing curly brace - The challenge asks you to use Doc for the
name
parameter, instead you have Snow White - Finally you're printing
Name Found: \(x)
which isn't required, you only need to print the word Found.
func search(#name: String) -> String? {
let names = ["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey"]
for n in names {
if n == name {
return n
}
}
return nil
}
if let x = search (name: "Doc") {
println("Found")
}
Happy coding!
Rahul Wadhavkar
1,080 PointsRahul Wadhavkar
1,080 PointsThanks Chris - I was confusing this with the next question. I got the answer...
Have one more question - with regards to the your point # 2 in the reply..the IF Let statement being inside the function. Was the issue the missing curly brace after return nil? also - I thought if its inside the function, the the function will not run after the return statement, we already have a return statement in 'return n'...so how is the function running to throw us back a a nil value? why is the function running the "return nil statement"...
sorry about the stupid questions, never done programning in my life, first time ever...just learning
Chris Shaw
26,676 PointsChris Shaw
26,676 PointsIn your original code you have your
IF
statement embedded within your function instead of after the ending curly brace, if you take a look at your code then mine you can see this very clearly as the return statement is followed directly by a closing curly brace whereas your code is followed by theIF
statement which wouldn't work.