Files
30-seconds-of-code/snippets_archive/heronArea.md
Kyle Begovich 9d461919a6 [FEATURE] Adding Heron's formula for area of any triangle (#813)
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.
2018-10-07 20:53:51 +02:00

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