Skip to content

Instantly share code, notes, and snippets.

@istro
Created June 14, 2012 03:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save istro/2927818 to your computer and use it in GitHub Desktop.
Save istro/2927818 to your computer and use it in GitHub Desktop.
Method to find the type of triangle by the length of three sides
# The problem:
# Write a triangle method that accepts three numbers as arguments. The method should return a print
# whether the three lengths form an equilateral, isosceles, or scalene triangle.
#
# We were just learning to define methods, and having learned if/else statements and touched on
# ternary statements, so we just wanted to make it as concise as we could with our limited knowledge :)
#
def triangle(side1, side2, side3)
if (side1 == side2 && side1 == side3) || (side1 + side2 <= side3 || side3 + side2 <= side1 || side1 + side3 <= side2)
(side1 == side2 && side1 == side3) ? puts "equilateral!" : puts "invalid!"
else
(side1 == side2 || side1 == side3 || side3 == side2) ? puts "isosceles!" : puts "scalene!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment