Exercism v3 launches on Sept 1st 2021. Learn more! πŸš€πŸš€πŸš€
Avatar of rootulp

rootulp's solution

to Triangle in the CoffeeScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Determine if a triangle is equilateral, isosceles, or scalene.

An equilateral triangle has all three sides the same length.

An isosceles triangle has at least two sides the same length. (It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.)

A scalene triangle has all sides of different lengths.

Note

For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side. See Triangle Inequality.

Dig Deeper

The case where the sum of the lengths of two sides equals that of the third is known as a degenerate triangle - it has zero area and looks like a single line. Feel free to add your own code/tests to check for degenerate triangles.

Refer to the Exercism CoffeScript page for getting started with CoffeeScript.

In order to run the test, you can run the test file from the exercise directory:

jasmine-node --coffee .

Source

The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

triangle_test.spec.coffee

Triangle = require './triangle'

describe "Triangle", ->
  it 'is equilateral with equal sides', ->
    triangle = new Triangle(2,2,2)
    expect(triangle.kind()).toBe 'equilateral'

  xit 'is equilateral for larger triangle', ->
    triangle = new Triangle(10,10,10)
    expect(triangle.kind()).toBe 'equilateral'

  xit 'is isosceles when last two sides equal', ->
    triangle = new Triangle(2,4,4)
    expect(triangle.kind()).toBe 'isosceles'

  xit 'is isosceles when first and last equal', ->
    triangle = new Triangle(4,2,4)
    expect(triangle.kind()).toBe 'isosceles'

  xit 'is isosceles when first two equal', ->
    triangle = new Triangle(4,4,2)
    expect(triangle.kind()).toBe 'isosceles'

  xit 'is isosceles with arbitrary sides', ->
    triangle = new Triangle(10,10,2)
    expect(triangle.kind()).toBe 'isosceles'

  xit 'is scalene when no sides equal', ->
    triangle = new Triangle(3,4,5)
    expect(triangle.kind()).toBe 'scalene'

  xit 'is scalene when larger as well', ->
    triangle = new Triangle(10,11,12)
    expect(triangle.kind()).toBe 'scalene'

  xit 'is scalene in decesending order', ->
    triangle = new Triangle(5,4,3)
    expect(triangle.kind()).toBe 'scalene'

  xit 'is legal when very small', ->
    triangle = new Triangle(.03,.06,.04)
    expect(triangle.kind()).toBe 'scalene'

  xit 'is illegal when a side is negative', ->
    expect(-> new Triangle(2,3,-5)).toThrow("negative sides are illegal")

  xit 'is illegal when violating triangle inequality', ->
    expect(-> new Triangle(1,1,3)).toThrow("violation of triangle inequality")

  xit 'is illegal when violating triangle inequality 2', ->
    expect(-> new Triangle(2,2,4)).toThrow("violation of triangle inequality")

  xit 'is illegal when violating triangle inequality 3', ->
    expect(-> new Triangle(7,3,2)).toThrow("violation of triangle inequality")
module.exports = class Triangle
  constructor: (@a, @b, @c) ->
    @sides = [@a, @b, @c]
    @isNegative()
    @isInequality()

  kind: ->
    return 'equilateral' if @isEquilateral()
    return 'isosceles'   if @isIsosceles()
    'scalene'

  isIsosceles: ->
    @a == @b ||
    @a == @c ||
    @b == @c

  isEquilateral: ->
    @a == @b &&
    @a == @c &&
    @b == @c

  isNegative: ->
    for side in @sides
      throw 'negative sides are illegal' if side < 0

  isInequality: ->
    if @a + @b <= @c ||
       @a + @c <= @b ||
       @b + @c <= @a
      throw 'violation of triangle inequality'

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?