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

iOS

Operators (iOS Swift) - Extra credit

Hi guys,

I am having a hard time figuring this one out via swift coding. Can anyone help me please?

Extra Credit

Assuming you have a triangle and are given the sizes of the two sides. Using the math operators taught in this stage calculate the value of the missing side.

You can use the Pythagorean theorem which states: a^2 = b^2 + c^2. Assume you are given b = 25 and c = 10.

3 Answers

Hi Pedro,

I'm pretty sure there are other ways of doing this. Here's mine

var b:Double = 25
var c:Double = 10

var sqA = b*b + c*c
var a = sqrt(sqA)

Testing with b = 3, c = 4, a should always be 5.

Makes sense, Robert! Thanks a lot for the answer!

Another quick question though: having the ":Double" specified on both var b and c doesnt make a difference, right? And could 'b' and 'c' be constants instead of vars in this case?

Thanks!

sqrt requires a double as parameter... So if you want to remove the :Double from b and c you need to cast sqA as a double. i.e.

var b = 25
var c = 10

var sqA = b*b + c*c
var a = sqrt(Double(sqA))

b and c could be constants, yes, I just used variables...

oh, I see. I still havent seen sqrt in the code lessons itself, but makes sense. Thanks for helping out!

Yeah, neither did I, but knew about it from previous courses.

I'm pretty sure you can go around it with more math, for me it was the shortest path :)