Document

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

<!

DOCTYPE html>

<html>

<head>

<title>Calculate Triangle Area</title>

</head>

<body>

<h1>Calculate Triangle Area</h1>

<label for="side1">Side 1:</label>

<input type="number" id="side1" /><br />

<label for="side2">Side 2:</label>

<input type="number" id="side2" /><br />

<label for="side3">Side 3:</label>

<input type="number" id="side3" /><br />

<!-- Add an onclick event to the button to call the calculateArea() function -->

<button onclick="calculateArea()">Calculate</button><br />

<label for="result">Area:</label>

<!-- Disable the input field for the result so the user can't edit it -->

<input type="number" id="result" disabled />

<script>

function calculateArea() {

const side1 = parseFloat(document.getElementById("side1").value);

const side2 = parseFloat(document.getElementById("side2").value);

const side3 = parseFloat(document.getElementById("side3").value);

// Calculate the semi-perimeter

const s = (side1 + side2 + side3) / 2;

// Calculate the area using Heron's formula

const area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));


// Display the area in the result input field

document.getElementById("result").value = area;

</script>

</body>

</html>

You might also like