If you missed the introductory post, it's here. For a list of previously solved katas, please refer to the bottom of this page.
If this is your first time seeing my post, please note - these katas are probably randomly assigned per user so please don't go into Codewars thinking we will have exactly the same user experience, I am just posting these in the order that I got them.
Kata #31
DESCRIPTION:
Implement a function that accepts 3 integer values a, b, c. The function should return true if a triangle can be built with the sides of given length and false in any other case.
(In this case, all triangles must have surface greater than 0 to be accepted).
Starting code:
function isTriangle(a,b,c)
{
return false;
}
My attempt that worked:
function isTriangle(a,b,c){
if(a > 0 && b > 0 && c > 0){
let s = (a + b + c)/2
let x = s-a
let y = s-b
let z = s-c
let sxyz = s*x*y*z
let area = Math.sqrt(sxyz)
if (area > 0){
return true
} else {
return false
}
} else {
return false
}
}
Took a long time to arrive at a solution that works because I somehow misunderstood the problem statement. In my mind I was thinking that if all arguments passed are at least above zero, then a triangle could be built but apparently I was wrong. Apparently there is a maximum number that the longest side could be so I had to google the formula (Heron's formula which I found here) and applied it to my code. Understanding the underlying logic of the formula was crucial to come up with a much shorter code that I have though.
Rank #1 in 'Best Practice':
function isTriangle(a,b,c)
{
return a + b > c && a + c > b && c + b > a;
}
Rank #2 with the enviable one-liner and use of Math.max():
var isTriangle = (a,b,c) => Math.max(a,b,c)<(a+b+c)/2
Thought about the approach of converting the integers into an array and sorting it but I got stuck, which rank #3 shows how it's done:
function isTriangle(a,b,c)
{
[a, b, c] = [a, b, c].sort((x, y) => x-y);
return a+b > c;
}
Rank #4:
function isTriangle(a,b,c) {
if(a > 0 && b > 0 && c > 0) {
if(a < b + c && b < a + c && c < a + b) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
Rank #5:
function isTriangle(a,b,c) {
var sides = [a, b, c].sort();
return (sides[0] + sides[1] > sides[2]);
}
Another "D'OH!" moment for sure. Feels like I have to go back to high school and re-learn geometry. Also maybe an indication that it's a bad idea to do several challenges at once because I might not be processing the problems as well as I should. 😵
That's it for Kata #31, stay tuned for more katas to be solved!
Link to Kata #1: Square(n) Sum
Link to Kata #2: Convert a Number to a String
Link to Kata #3: DNA to RNA Conversion
Link to Kata #4: Remove First and Last Character
Link to Kata #5: MakeUpperCase
Link to Kata #6: Total amount of points
Link to Kata #7: A Needle in the Haystack
Link to Kata #8: Sum of positive
Link to Kata #9: Basic Mathematical Operations
Link to Kata #10: Beginner - Reduce but Grow
Link to Kata #11: Square Every Digit
Link to Kata #12: Friend or Foe?
Link to Kata #13: Grasshopper - Summation
Link to Kata #14: Get the Middle Character
Link to Kata #15: Descending Order
Link to Kata #16: String ends with?
Link to Kata #17: Sum of two lowest positive integers
Link to Kata #18: Sum of odd numbers
Link to Kata #19: Find the next perfect square!
Link to Kata #20: Reversed Strings
Link to Kata #21: Jaden Casing Strings
Link to Kata #23: Returning Strings
Link to Kata #24: Opposite number
Link to Kata #25: Are You Playing Banjo?
Link to Kata #26: Beginner Series #1 School Paperwork
Link to Kata #27: Remove String Spaces