# Khan/khan-exercises

Fetching contributors…
Cannot retrieve contributors at this time
61 lines (59 sloc) 3.2 KB
 Heron's formula
randomTriangleAngles.triangle() randRange( 3, 7 ) + random() AREA * AREA
What is the area of this triangle? ( Round to two decimal places )
init({ range: [ [-1, 12 ], [ -7, 2.5 ] ] }) var trA = new Triangle( [ 3, -3 ], ANGLES ,AREA, {} ); trA.boxOut( [ [ [ -10, 2.3 ], [ 10, 2.3 ] ] ] , [ 0, -0.7 ] ); trA.boxOut( [ [ [ -1, -10 ], [ -1, 10 ] ] ] , [ 0.7, 0 ] ); trA.boxOut( [ [ [ 11.5, -10 ], [ 11.5, 10 ] ] ] , [ -0.7, 0 ] ); trA.draw(); trA.labels = { "sides" : trA.niceSideLengths }; trA.drawLabels(); SIDES = trA.niceSideLengths; S = ( ( SIDES[ 0 ] + SIDES[ 1 ] + SIDES[ 2 ] ) / 2 ).toFixed( 2 ); ANS = sqrt(S *( S -SIDES[ 0 ] ) * ( S - SIDES[ 1 ] ) * ( S - SIDES[ 2 ] ) ).toFixed( 2 ); \$( "#ans" ).html( ANS ) ;

We know all sides of this triangle, so we can use Heron's formula to calculate the area.

Heron's formula states that the area of a triangle A=\sqrt{s(s-a)(s-b)(s-c)}

s = \dfrac{ a + b + c }{ 2 }

s = \dfrac{ SIDES[ 0 ] + SIDES[ 1 ] + SIDES[ 2 ] }{ 2 }

s = \dfrac{ ( SIDES[ 0 ] + SIDES[ 1 ] + SIDES[ 2 ] ).toFixed( 1 ) }{ 2 }

s = S

A = \sqrt{ S \cdot ( S - SIDES[ 0 ] ) \cdot ( S - SIDES[ 1 ] ) \cdot ( S - SIDES[ 2 ] ) }

A = \sqrt{ S \cdot ( S - SIDES[ 0 ] ).toFixed( 2 ) \cdot ( S - SIDES[ 1 ] ).toFixed( 2 ) \cdot ( S - SIDES[ 2 ] ).toFixed( 2 ) }

A = ANS

Something went wrong with that request. Please try again.