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 trial
kaitlanlang
3,042 Points(Swift topic) Exercise:isDivisible function (use of variables initiated in "if let" and called in println)
Below is my answer to the challenge, it worked fine, giving a result of "is divisible" or "is not divisible" i wanted to incorporate the numbers sent to function in the println statement
func isDivisible(#firstNumber:Int, #secondNumber:Int) -> Bool? {
if (firstNumber % secondNumber) == 0 {
return true
} else
{
return nil
}
}
if let result = isDivisible(firstNumber: 4, secondNumber: 4){
println("is divisible")
//Instead of above line I wanted something like this for example
// if let result = isDivisible(firstNumber: 4, secondNumber: 4){
// println("\(firstNumber) is divisible by \(secondNumber)")
} else {
println("is NOT divisble")
}
So the commented out code i tried in place of code that worked for the challenge. It would not allow the variables in println I have a very vague idea why but need clarity. Is it because these variables existence is conditional on the "if let" and as such can't be used? If some one can clear this up that would be great, also showing an alternate way to if it exists to achieve this in an "if let" "println" combination :)
1 Answer
Jeremy Hayden
1,740 PointsKaitlan,
The reason that you can not
println("\(firstNumber) is divisible by \(secondNumber)")
is that firstNumber and secondNumber have not been declared as variables or constants yet. As a variable they simply do not exist at this point.
It seems like they have been, because you can use this line
if let result = isDivisible(firstNumber: 4, secondNumber: 4)
But this line is not using them as variables, but as reference names for the function.
Modifying your code as follows will give you a result similar to what you are looking for.
func isDivisible(#firstNumber:Int, #secondNumber:Int) -> Bool? {
if (firstNumber % secondNumber) == 0 {
return true
} else
{
return nil
}
}
//if let result = isDivisible(firstNumber: 4, secondNumber: 4){
//println("is divisible")
//Instead of above line I wanted something like this for example
var firstNumber = 4
var secondNumber = 4
if let result = isDivisible(firstNumber: firstNumber, secondNumber: secondNumber){
println("\(firstNumber) is divisible by \(secondNumber)")
} else {
println("is NOT divisble")
}
kaitlanlang
3,042 Pointskaitlanlang
3,042 PointsThat makes complete sense particularly when you said "but this line is not using them as variables, but as reference names for the function."
Declaring them first and inserting those declared variables into function call is the way to go :) thank you very much.