ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# cedricvaneer's solution

## to Triangle in the CFML Track

Published at Jan 22 2020 · 0 comments
Instructions
Test suite
Solution

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.

To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, `cd` into the exercise folder and run the following:

``````box task run TestRunner
# Or start up a test watcher that will rerun when files change
``````

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

## 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.

### SolutionTest.cfc

``````component extends="TriangleTest" {

function beforeAll(){
SUT = createObject( 'Solution' );
}

}``````

### TriangleTest.cfc

``````component extends="testbox.system.BaseSpec" {

function beforeAll(){
SUT = createObject( 'Triangle' );
}

function run(){

describe( "My Triangle class", function(){

describe( 'returns true if the triangle is equilateral', function(){

it( 'true if all sides are equal', function(){
expect( SUT.equilateral( sides=[2, 2, 2] ) ).toBeTrue();
});

it( 'false if any side is unequal', function(){
expect( SUT.equilateral( sides=[2, 3, 2] ) ).toBeFalse();
});

it( 'false if no sides are equal', function(){
expect( SUT.equilateral( sides=[5, 4, 6] ) ).toBeFalse();
});

it( 'All zero sides are illegal, so the triangle is not equilateral', function(){
expect( SUT.equilateral( sides=[0, 0, 0] ) ).toBeFalse();
});

it( 'sides may be floats', function(){
expect( SUT.equilateral( sides=[0.5, 0.5, 0.5] ) ).toBeTrue();
});

});

describe( 'returns true if the triangle is isosceles', function(){

it( 'true if last two sides are equal', function(){
expect( SUT.isosceles( sides=[3, 4, 4] ) ).toBeTrue();
});

it( 'true if first two sides are equal', function(){
expect( SUT.isosceles( sides=[4, 4, 3] ) ).toBeTrue();
});

it( 'true if first and last sides are equal', function(){
expect( SUT.isosceles( sides=[4, 3, 4] ) ).toBeTrue();
});

it( 'equilateral triangles are also isosceles', function(){
expect( SUT.isosceles( sides=[4, 4, 4] ) ).toBeTrue();
});

it( 'false if no sides are equal', function(){
expect( SUT.isosceles( sides=[2, 3, 4] ) ).toBeFalse();
});

it( 'Sides that violate triangle inequality are not isosceles, even if two are equal', function(){
expect( SUT.isosceles( sides=[1, 1, 3] ) ).toBeFalse();
});

it( 'sides may be floats', function(){
expect( SUT.isosceles( sides=[0.5, 0.4, 0.5] ) ).toBeTrue();
});

});

describe( 'returns true if the triangle is scalene', function(){

it( 'true if no sides are equal', function(){
expect( SUT.scalene( sides=[5, 4, 6] ) ).toBeTrue();
});

it( 'false if all sides are equal', function(){
expect( SUT.scalene( sides=[4, 4, 4] ) ).toBeFalse();
});

it( 'false if two sides are equal', function(){
expect( SUT.scalene( sides=[4, 4, 3] ) ).toBeFalse();
});

it( 'Sides that violate triangle inequality are not scalene, even if they are all different', function(){
expect( SUT.scalene( sides=[7, 3, 2] ) ).toBeFalse();
});

it( 'sides may be floats', function(){
expect( SUT.scalene( sides=[0.5, 0.4, 0.6] ) ).toBeTrue();
});

});

});

}

}``````
``````component {

public boolean function isTriangle( required array sides ) {
if (sides[1] == 0 OR sides[2] == 0 OR sides[3] == 0) {
return false;
}
if (sides[1] > sides[2]+sides[3] OR sides[2] > sides[1]+sides[3] OR sides[3] > sides[1]+sides[2]) {
return false;
}
return true;
}

public boolean function equilateral( required array sides ) {
if (sides[1] == sides[2] AND sides[2] == sides [3] AND sides[1] != 0) {
return true;
}
return false;
}

public boolean function isosceles( required array sides ) {
var isoscele = false;
if (sides[1] == sides[2] OR sides[1] == sides[3] OR sides[2] == sides[3]) {
isoscele = true;
}
return isTriangle(arguments.sides) AND isoscele;
}

public boolean function scalene( required array sides ) {
if (sides[1] == sides[2] OR sides[1] == sides[3] OR sides[2] == sides[3]) {
return false;
}
return isTriangle(arguments.sides);
}

}``````