Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
Fórmula de garzas en javascript y html.

Tengo que encontrar las áreas de un triángulo usando la fórmula de Heron . Escribí este código pero no está calculando el área correctamente.
Por ejemplo, ingreso sideA= 5 , sideB= 4 , sideC= 3 y se supone que la respuesta es 6, pero en su lugar obtengo 72088

¿Alguien puede ayudarme a solucionar esto y explicar por qué sucede?

 function Triangle(sideA, sideB, sideC) { this.sideA = sideA; this.sideB = sideB; this.sideC = sideC; this.semiP = ((this.sideA + this.sideB + this.sideC) / 2); this.area = function() { return (Math.sqrt(this.semiP * ((this.semiP - this.sideA) * (this.semiP - this.sideB) * (this.semiP - this.sideC)))); } } function calculate() { var sideA = document.getElementById('sideA').value; var sideB = document.getElementById('sideB').value; var sideC = document.getElementById('sideC').value; var myTriangle = new Triangle(sideA, sideB, sideC); document.getElementById('results').innerHTML = "Area: " + myTriangle.area(); }
 <p> Enter length of side a <input type='text' id="sideA" /> <br> <br> Enter length of side b <input type='text' id="sideB" /> <br> <br> Enter length of side c <input type='text' id="sideC" /> <div id="results"></div> <button type="button" onclick="calculate()">Calculate</button>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El problema que está teniendo es que las variables sideA, B y C son todas del tipo STRING y nada numérico. Hay algunas soluciones diferentes, en mi respuesta elegí usar parseFloat para permitirle tener puntos decimales como entrada, así como números enteros.

 function Triangle(sideA, sideB, sideC) { this.sideA = parseFloat(sideA); this.sideB = parseFloat(sideB); this.sideC = parseFloat(sideC); this.semiP = ((this.sideA + this.sideB + this.sideC) / 2); this.area = function() { return (Math.sqrt(this.semiP * ((this.semiP - this.sideA) * (this.semiP - this.sideB) * (this.semiP - this.sideC)))); } } function calculate() { var sideA = document.getElementById('sideA').value; var sideB = document.getElementById('sideB').value; var sideC = document.getElementById('sideC').value; var myTriangle = new Triangle(sideA, sideB, sideC); document.getElementById('results').innerHTML = "Area: " + myTriangle.area(); }
 <p> Enter length of side a <input type='text' id="sideA" /> <br> <br> Enter length of side b <input type='text' id="sideB" /> <br> <br> Enter length of side c <input type='text' id="sideC" /> <div id="results"></div> <button type="button" onclick="calculate()">Calculate</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!