File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Source-Code/WeightConverter Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ document . addEventListener ( 'DOMContentLoaded' , ( ) => {
2+ const convertButton = document . getElementById ( 'convertButton' ) ;
3+ const resetButton = document . getElementById ( 'resetButton' ) ;
4+
5+ function convert ( ) {
6+ const kilograms = parseFloat ( document . getElementById ( 'kgs' ) . value ) ;
7+
8+ if ( Number . isNaN ( kilograms ) || kilograms <= 0 ) {
9+ window . alert ( 'Weight must be greater than zero!!' ) ;
10+ } else {
11+ const gramsResult = kilograms * 1000 ;
12+ const poundsResult = kilograms * 2.20462 ;
13+ const ouncesResult = kilograms * 35.274 ;
14+
15+ document . getElementById ( 'grams' ) . value = gramsResult . toFixed ( 2 ) ;
16+ document . getElementById ( 'pounds' ) . value = poundsResult . toFixed ( 3 ) ;
17+ document . getElementById ( 'ounces' ) . value = ouncesResult . toFixed ( 2 ) ;
18+ }
19+ }
20+
21+ function clearResults ( ) {
22+ document . getElementById ( 'grams' ) . value = '' ;
23+ document . getElementById ( 'pounds' ) . value = '' ;
24+ document . getElementById ( 'ounces' ) . value = '' ;
25+ }
26+
27+ convertButton . addEventListener ( 'click' , ( ) => {
28+ convert ( ) ;
29+ } ) ;
30+
31+ resetButton . addEventListener ( 'click' , ( ) => {
32+ document . getElementById ( 'converter' ) . reset ( ) ;
33+ clearResults ( ) ;
34+ } ) ;
35+ } ) ;
You can’t perform that action at this time.
0 commit comments