Heron's Formula for Area of any (valid) triangle. ## Description Adding heronArea.js and adding the corresponding entry in tag_database. More info on Heron's formula (and why it works) here: https://en.wikipedia.org/wiki/Heron%27s_formula. This is exceptionally helpful for simplifying any math being done inside your JS, and abstracts away any altitude calculations being done before the (1/2)bh calculation often used instead. ## PR Type - [x] Snippets, Tests & Tags (new snippets, updated snippets, re-tagging of snippets, added/updated tests) - [ ] Scripts & Website & Meta (anything related to files in the [scripts folder](https://github.com/30-seconds/30-seconds-of-code/tree/master/scripts), how the repository's automated procedures work and the website) - [ ] Glossary & Secondary Features (anything related to the glossary, such as new or updated terms or other secondary features) - [ ] General, Typos, Misc. & Meta (everything else, typos, general stuff and meta files in the repository - e.g. the issue template) ## Guidelines - [x] I have read the guidelines in the [CONTRIBUTING](https://github.com/30-seconds/30-seconds-of-code/blob/master/CONTRIBUTING.md) document.
574 B
574 B
heronArea
Returns the area of a triangle using only the 3 side lengths, Heron's formula. Assumes that the sides define a valid triangle. Does NOT assume it is a right triangle.
More information on what Heron's formula is and why it works available here: https://en.wikipedia.org/wiki/Heron%27s_formula.
Uses Math.sqrt() to find the square root of a value.
const heronArea = (side_a, side_b, side_c) => {
const p = (side_a + side_b + side_c) / 2
return Math.sqrt(p * (p-side_a) * (p-side_b) * (p-side_c))
};
heronArea(3, 4, 5); // 6