JavaScript Exercises Basic#2 - Forming a triangle with three sticks

JavaScript Exercises Basic#2 - Forming a triangle with three sticks

You have three sticks of length a, b and c. Write a program which determines if you can form a triangle out of those sticks.

Solution

          /*
            To form a triangle with three sticks, the length of the longest stick should be less than the sum of other two.
            1. Declare variables: max(length of the longest stick), sum(length of a + b + c)
            2. Compare the length of three sticks and store longest one in the variable max
            3. Print "YES" if max is less than (sum - max) 
               Otherwise print "NO"

            */

            function solution(a, b, c){

                let answer;
                let max;
                let sum = a + b + c;

                if (a > b) max = a;
                    else max = b;
                if (c > max) max = c;

                if ((sum - max) > max) answer = "YES";
                    else answer = "NO";

                return answer;    
            }

Result

Screenshot 2022-04-12 at 19.48.30.png