11// Attach an event listener to the 'sub' element when clicked
2- document . getElementById ( 'sub' ) . addEventListener ( 'click' , function ( ) {
3- // Get the height and weight values from the input fields
4- let h = document . getElementById ( 'height' ) . value ;
5- let w = document . getElementById ( 'weight' ) . value ;
6-
7- // Convert height to meters
8- h /= 100.0 ;
9-
10- // Calculate BMI
11- let bmi = w / ( h * h ) ;
12-
13- // Round BMI to two decimal places
14- bmi = parseFloat ( bmi ) . toFixed ( 2 ) ;
15-
16- let img ;
17- let data = '' ;
18-
19- // Determine the BMI category and corresponding image
20- if ( bmi < 18.5 ) {
21- data = 'You Are Underweight' ;
22- img = "./assets/underweight.jpg" ;
23- } else if ( bmi >= 18.5 && bmi < 25 ) {
24- data = 'You Are Healthy' ;
25- img = "./assets/healthy.jpg" ;
26- } else if ( bmi >= 25 && bmi < 30 ) {
27- data = 'You Are Overweight' ;
28- img = "./assets/overweight.jpg" ;
29- } else if ( bmi >= 30 ) {
30- data = 'You Are Obese' ;
31- img = "./assets/obese.jpg" ;
32- } else {
33- data = 'Please Enter a Valid Input' ;
34- }
35-
36- // Set the source attribute of the 'body' element to display the appropriate image
37- document . getElementById ( 'body' ) . setAttribute ( "src" , img ) ;
38-
39- // Display the result and BMI information
40- document . getElementById ( 'res' ) . innerHTML = ` ${ data } .` ;
41- document . getElementById ( 'result' ) . innerHTML = `Your BMI is <strong>${ bmi } </strong>.` ;
42-
43- // Show the 'info' card
44- document . getElementById ( 'info' ) . setAttribute ( "class" , "card show" ) ;
45- } ) ;
46-
2+
3+ const inputClearer = ( ) => {
4+ const hElement = document . getElementById ( "height" ) ;
5+ const wElement = document . getElementById ( "weight" ) ;
6+ hElement . value = "" ;
7+ wElement . value = "" ;
8+ } ;
9+
10+ document . getElementById ( "sub" ) . addEventListener ( "click" , function ( ) {
11+ // Get the height and weight values from the input fields
12+ let h = document . getElementById ( "height" ) . value ;
13+ let w = document . getElementById ( "weight" ) . value ;
14+
15+ let hVal = parseInt ( h ) ;
16+ let wVal = parseInt ( w ) ;
17+
18+ if ( h . length === 0 || w . length === 0 ) {
19+ alert ( "Empty inputs detected! Plz enter both height and weight" ) ;
20+ inputClearer ( ) ;
21+ return ;
22+ }
23+ if ( hVal === 0 ) {
24+ alert ( "Zero height entered! Enter a valid value" ) ;
25+ inputClearer ( ) ;
26+ return ;
27+ }
28+
29+ if ( hVal < 0 || wVal < 0 ) {
30+ alert ( "Negative Values found Enter valid inputs!" ) ;
31+ inputClearer ( ) ;
32+ return ;
33+ }
34+
35+ // Convert height to meters
36+ h /= 100.0 ;
37+
38+ // Calculate BMI
39+ let bmi = w / ( h * h ) ;
40+
41+ // Round BMI to two decimal places
42+ bmi = parseFloat ( bmi ) . toFixed ( 2 ) ;
43+
44+ let img ;
45+ let data = "" ;
46+
47+ // Determine the BMI category and corresponding image
48+ if ( bmi < 18.5 ) {
49+ data = "You Are Underweight" ;
50+ img = "./assets/underweight.jpg" ;
51+ } else if ( bmi >= 18.5 && bmi < 25 ) {
52+ data = "You Are Healthy" ;
53+ img = "./assets/healthy.jpg" ;
54+ } else if ( bmi >= 25 && bmi < 30 ) {
55+ data = "You Are Overweight" ;
56+ img = "./assets/overweight.jpg" ;
57+ } else if ( bmi >= 30 ) {
58+ data = "You Are Obese" ;
59+ img = "./assets/obese.jpg" ;
60+ } else {
61+ data = "Please Enter a Valid Input" ;
62+ }
63+
64+ // Set the source attribute of the 'body' element to display the appropriate image
65+ document . getElementById ( "body" ) . setAttribute ( "src" , img ) ;
66+
67+ // Display the result and BMI information
68+ document . getElementById ( "res" ) . innerHTML = ` ${ data } .` ;
69+ document . getElementById (
70+ "result"
71+ ) . innerHTML = `Your BMI is <strong>${ bmi } </strong>.` ;
72+
73+ // Show the 'info' card
74+ document . getElementById ( "info" ) . setAttribute ( "class" , "card show" ) ;
75+ } ) ;
0 commit comments