# Daily Challenge #84 - Third Angle of a Triangle

You are given two angles of a triangle in degrees.

Write a function to return the third angle. Only positive integers will be tested.

Sample Tests:

thirdAngle(30, 60) thirdAngle(60, 60) thirdAngle(43, 78) thirdAngle(10, 20)

This challenge comes from user5651159. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

### Discussion

ANSI C

#include <math.h>

double third_angle ( double first_angle , double second_angle ) {
first_angle = fmod( first_angle , 360.0 );  // Get absolute
second_angle = fmod( second_angle , 360.0 );

if ( first_angle + second_angle >= 180 )
return 0.0;  // That's not a triangle! OwO

return ( 180 - first_angle - second_angle );
}

int main ( int argc , char ** argv ) {
if (
( int ) third_angle( 30 , 60 ) == 90 &&
( int ) third_angle( 60 , 60 ) == 60 &&
( int ) third_angle( 43 , 78 ) == 59 &&
( int ) third_angle( 10 , 20 ) == 150 )
return 0;  // It works!

return 101;  // Or not
}



Run with ~$gcc -lm <file_name> && ./a.out My solution in js const thirdAngle = (angle1, angle2) => (angle1 && angle2 && (angle3 = 180 - (angle1 + angle2)) > 0) ? angle3 : 'Invalid - Sum of all angles of a triangle should be 180 degrees';  What happens if I enter thirdAngle(0,0)? Good catch. Edited. But the challenge is to deal with positive integers so didn't take 0 into account as it is neither positive nor negative integer. True enough. But why code purely for specs when coding for the edge and error cases takes just a few more minutes? In Go! func thirdAngle(a, b int) (int, error) { if (a + b) >= 180 { return 0, errors.New("Input angles sum is greater than or equal to 180 degress, this cannot be a triangle") } if a == 0 || b == 0 { return 0, errors.New("Input angle cannot be zero, cannot be a triangle") } angle := 360 - a - b // sum of angles must be 360 degress return angle, nil }  Go Playground # Solution in APL (using Dyalog APL) thirdAngle←{r←180-+/⍵ ⋄ (r<0)∨0∊⍵: 'This is not a triangle!' ⋄ r}  ## Tests:  thirdAngle 30 60 90 thirdAngle 60 60 60 thirdAngle 43 78 59 thirdAngle 10 20 150  Try it online! This oughta do it for Elixir. defmodule Triangle do def third_angle(a, b) when is_integer(a + b) # will not qualify if either isn't an integer and a + b < 180 and a > 0 and b > 0 do 180 - a - b end end  Keeping it simple in bash function thirdAngle { echo$((180 - $1 -$2))
}


Running through the examples on the cmd:

> thirdAngle 30 60
90

> thirdAngle 60 60
60

> thirdAngle 43 78
59

> thirdAngle 10 20
150


BTW the challenge on Kata only tests for valid triangles,

function otherAngle(a, b) {
return 180-(a+b);
}


was sufficient in JS

Elm

import List exposing (all, sum)
import Maybe exposing (andThen, map)

isAngle : Int -> Bool
isAngle angle =
angle > 0 && angle < 180

maybeAngles : List Int -> Maybe (List Int)
maybeAngles angles =
if all isAngle angles && sum angles < 180 then
Just angles

else
Nothing

third : Int -> Int -> Maybe Int
third first second =
maybeAngles [ first, second ]
|> map (sum >> (-) 180)


Test it online

Here.

function thirdAngle(a,b) {
return 180-a-b;
}


What happens if I enter thirdAngle(0,0)?

you are right, I just wrote it very fast and I did not think about this case.
TDD will help a lot in these situations.