@@ -3043,32 +3043,43 @@ <h3 itemprop="eduQuestionType" style="margin:7px">Area of a Circle Segment</h3>
30433043< input id ="parent-radius " type ="number " step ="any " oninput ="segmentArea() ">
30443044< br >
30453045< script >
3046- function segmentArea ( ) {
3047- let hVal = document . getElementById ( 'segment-height' ) . value ;
3048- let rVal = document . getElementById ( 'parent-radius' ) . value ;
3049- let lVal = document . getElementById ( 'chord-length' ) . value ;
3046+ let calculated = false ;
3047+
3048+ function segmentArea ( ) {
3049+ if ( calculated ) {
3050+ // Clear everything on next input after a calculation
3051+ document . getElementById ( 'segment-height' ) . value = "" ;
3052+ document . getElementById ( 'parent-radius' ) . value = "" ;
3053+ document . getElementById ( 'chord-length' ) . value = "" ;
3054+ document . getElementById ( 'segment-area' ) . innerText = "" ;
3055+ calculated = false ; // reset flag
3056+ return ;
3057+ }
30503058
3051- // Treat empty string as NaN
3052- let h = hVal === "" ? NaN : parseFloat ( hVal ) ;
3053- let r = rVal === "" ? NaN : parseFloat ( rVal ) ;
3054- let l = lVal === "" ? NaN : parseFloat ( lVal ) ;
3059+ let h = parseFloat ( document . getElementById ( 'segment-height' ) . value ) ;
3060+ let r = parseFloat ( document . getElementById ( 'parent-radius' ) . value ) ;
3061+ let l = parseFloat ( document . getElementById ( 'chord-length' ) . value ) ;
30553062
30563063 let known = [ ! isNaN ( h ) , ! isNaN ( r ) , ! isNaN ( l ) ] . filter ( Boolean ) . length ;
30573064 if ( known < 2 ) {
30583065 document . getElementById ( 'segment-area' ) . innerText = "" ;
3059- return ; // don’t auto‑fill anything
3066+ return ;
30603067 }
30613068
3062- // Derive missing property only once we have exactly 2 inputs
3069+ // Case 1: height + radius known → derive length
30633070 if ( ! isNaN ( h ) && ! isNaN ( r ) && isNaN ( l ) ) {
30643071 let angle = Acos ( ( r - h ) / r ) ;
30653072 l = 2 * r * sin ( angle ) ;
30663073 document . getElementById ( 'chord-length' ) . value = l . toFixed ( 5 ) ;
30673074 }
3075+
3076+ // Case 2: height + length known → derive radius
30683077 if ( ! isNaN ( h ) && ! isNaN ( l ) && isNaN ( r ) ) {
30693078 r = ( l ** 2 + 4 * h ** 2 ) / ( 8 * h ) ;
30703079 document . getElementById ( 'parent-radius' ) . value = r . toFixed ( 5 ) ;
30713080 }
3081+
3082+ // Case 3: length + radius known → derive height
30723083 if ( ! isNaN ( l ) && ! isNaN ( r ) && isNaN ( h ) ) {
30733084 h = r - Math . sqrt ( r ** 2 - ( l / 2 ) ** 2 ) ;
30743085 document . getElementById ( 'segment-height' ) . value = h . toFixed ( 5 ) ;
@@ -3085,15 +3096,18 @@ <h3 itemprop="eduQuestionType" style="margin:7px">Area of a Circle Segment</h3>
30853096 let angle = Acos ( ( r - h ) / r ) ;
30863097 let area = angle * r ** 2 - ( r - h ) * ( l / 2 ) ;
30873098
3088- // Output
3099+ // Output with semicircle reminders
30893100 if ( h === r || h === l / 2 || l === 2 * r ) {
30903101 document . getElementById ( 'segment-area' ) . innerText =
30913102 `Semicircle area: ${ area . toFixed ( 5 ) } square units` ;
30923103 } else {
30933104 document . getElementById ( 'segment-area' ) . innerText =
30943105 `Area: ${ area . toFixed ( 5 ) } square units` ;
30953106 }
3096- }
3107+
3108+ // Mark that a calculation has been completed
3109+ calculated = true ;
3110+ }
30973111</ script >
30983112 < p id ="segment-area "> </ p >
30993113</ div >
0 commit comments