JavaScript and the Heron Formula

Is there a way to implement the Heron formula and semiperimeter with 3 sides in JavaScript? The function should return the area of the triangle based on input values of A, B, and C. Additionally, it needs to check if the sum of two sides is greater than the third side (a+b>c) and only accept numerical inputs via forms instead of letters.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Triangle Area Calculator</title>
</head>
    <body>
<script>
        function calculateArea(sideA, sideB, sideC) {
            var result;

            result = Math.sqrt(((sideA + sideB + sideC) / 2 - sideA) * ((sideA + sideB + sideC) / 2 - sideB) * ((sideA + sideB + sideC) / 2 - sideC));

            return result;
        }

        var triangleArea = calculateArea(3, 4, 5);
        document.write("The area of the triangle is: " + triangleArea);

        </script>
</body>
</html>

Answer №1

It appears that you are seeking information on Heron's formula implemented in Javascript, although the question is somewhat unclear.

Heron's formula, as defined by A = \sqrt{s(s-a)(s-b)(s-c)}, can be found on Wikipedia.

Here is a Javascript function to calculate the area using Heron's formula:

function computeArea(a, b, c, s) {
    return Math.sqrt(s * ((s - a) * (s - b) * (s - c)));
}

Answer №2

Learn more about Heron's formula.

function calculateArea( sideA, sideB, sideC ) {
  const semiPerimeter = (sideA + sideB + sideC) / 2
  return Math.sqrt( semiPerimeter * (semiPerimeter - sideA) * (semiPerimeter - sideB) * (semiPerimeter - sideC) )
}

document.write(calculateArea(3,4,5))

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Locate the nested route within one of the child components in React Router that corresponds to a specific id

Picture this scenario where I have a list of routes: const routes = [{ id: "1", path: "animals", children: [{ id: "1.1", path: "birds", children: [{ id: "1.1.1", path: "co ...

Eliminate the .html extension from the URL and initiate a redirect

I need assistance removing the .html extension from URLs that are displayed in the address bar. To achieve this, I used the following code: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f This solution ...

What is the best way to retrieve the inner HTML or text content of a tag?

How can I retrieve the text value of an anchor tag in HTML using only golang.org/x/net/html, without external libraries? In my code sample below, I am able to extract the values of href and title with html.ElementNode. However, I need a way to specifically ...

Adaptive Table Layout that Creates a Page-breaking Design

I have a page layout with three columns: a fixed-width left-hand navigation, a responsive content column, and a fixed-width right-hand navigation. The issue arises when the content in the middle column includes HTML tables that sometimes exceed the availab ...

Display or conceal various content within div elements using multiple buttons

I have a set of 5 image buttons, each meant to trigger different content within a div. When the page loads, there should be default content displayed in the div that gets replaced by the content of the button clicked. The previously displayed content shoul ...

Enhance the speed of my Tumblr page by using JavaScript for personalized styling tweaks

-- Hey there! Dear Reader, if navigating Tumblr isn't your cup of tea, let me know and I'll provide more detailed instructions! I recently ran into some glitches on my Tumblr page while using the default theme 'Optica'. Whenever I tri ...

The Redux Toolkit Slice Reducer fails to function properly when incorporating an extra Reducer that is not compatible

I am relatively new to the world of Redux and have been attempting to use RTK right from the start. It has been quite a challenging and confusing experience for me so far. Recently, I decided to include a standard Reducer instead of an extraReducer in my ...

Failed PHP AJAX request

Hello, I'm new to working with AJAX and am attempting to establish a connection between AJAX and PHP. Here is the code snippet I'm using: file1.php <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jqu ...

Send an AJAX request to the specified `httpost` action method without any page refresh to load the

Today I have an interesting challenge that's been a brain teaser for me. While I may not be an expert in ASP.Net MVC4, I'm excited to tackle something new. The ultimate goal is to create a dynamic tree view for partial pages within a standard pag ...

What is the method for retrieving the fixed information?

I am currently working on a project that involves creating a course-rating API. We have been given a completed angular application to work with, and our task is to set up the routes without making any changes to the Angular app. One of the initial tasks a ...

Is it possible for me to send transactions asynchronously using polkadot-js?

After thoroughly going through the official documentation, I stumbled upon a page discussing how to transfer using polkadot-js const transfer = api.tx.balances.transfer(BOB, 12345); const hash = await transfer.signAndSend(alice); I am curious if it' ...

Encountering 'undefined' issue with find operation in mongoDB

Seeking assistance to utilize all available values in my code. bericht.find({ room: room }).toArray(function(err, docs) { assert.equal(err, null); str2 = str2 + docs.message; The function I'm using can successfully loca ...

What is the most effective method for optimizing websites that do not respond to changes in window size

I've developed a website that is not responsive (it's more of an "experimental/artistic" site with a lot going on the screen, making it difficult to make it responsive..) I have decided not to cater for mobile phones, but I would like the site t ...

Strong tags within MFC

Is it possible to create labels in MFC (Static text) that contain both bold and non-bold text? For instance, something like this: "I want my label to have a mix of styles like this" Any suggestions on how to achieve this? I am aware that I can change ...

Retrieve a specified quantity of JSON data from an external API

Dealing with a recently received API from an external source: (please note: the data returned is extensive) I'm aware that I can retrieve this large object by using the following method: $.getJSON('https://www.saferproducts.gov/RestWebServices ...

Is there a way to modify the space bar and enter key functionality exclusively for the third button, rather than applying it to all buttons?

https://jsfiddle.net/ffkwgddL/ In the code, I have three buttons named first-button, second-button, and third-button. If the first-button (introduction) is clicked and then the space bar is pressed, it functions as if the first button was clicked again. H ...

Disable the 'bouncy scrolling' feature for mobile devices

Is there a way to prevent the bouncy scrolling behavior on mobile devices? For example, when there is no content below to scroll, but you can still scroll up and then it bounces back when released. Here is my HTML structure: <html> <body> ...

Creating a Node.js API using express and mysql to retrieve various data including record count, page number, and implementing pagination functionality

How can I efficiently retrieve and display total_record_count, page_number, page_size, total_pages, has_more information in the response while implementing pagination given that I am limiting my query to 100 results? What would be the most effective approa ...

Retrieving the text of a button using Protractor

After developing a unique locator to identify an element utilizing the ng-click method, I was able to retrieve a button reference from my Document Object Model (DOM). this.button = element(by.ngClick('login()')); Now, my goal is to extract the ...

Unable to assign the 'id' property within Datatable

Hello there, I am currently using a datatable and fetching data via AJAX. For each data entry, I add a row to my datatable. However, I keep encountering an error that reads: "Uncaught TypeError: Cannot set property 'id' of null in line code " ...