Hi Everone, welcome to my blog in this blog post i will share the code of JavaScript practice project "BMI calculator" for beginners along with explaination. You can copy the code by clicking the button just above the code. Read the explaiantion for better understanding.
let's get started with index.html file:
index.html
style.css:
index.js:
Here is the explaination:
It declares variables btn, bmiresult, and weightcondition that will reference HTML elements in the document.
It defines a function called calculate() that will be executed when a button with the id "btn" is clicked.
Inside the calculate() function, it retrieves the values of the height and weight inputs from the HTML elements with the corresponding ids.
It calculates the BMI by dividing the weight (in kilograms) by the square of the height (in meters).
It assigns the calculated BMI value to the bmiresult element's value property, which will update the displayed result in the HTML.
It uses conditional statements to determine the weight condition based on the calculated BMI.
If the BMI is less than 18.5, it sets the innerText of the weightcondition element to "Under Weight".
If the BMI is between 18.5 and 24.9 (inclusive), it sets the innerText to "Normal Weight".
If the BMI is between 25 and 29.9 (inclusive), it sets the innerText to "OverWeight".
If the BMI is greater than or equal to 30, it sets the innerText to "Obesity".
Finally, it adds an event listener to the button element. When the button is clicked, the calculate() function will be called, triggering the BMI calculation and weight condition determination.
FINAL WORDS:
In conclusion, the provided code implements a BMI (Body Mass Index) calculator using JavaScript. The BMI is calculated based on the user's input of height and weight, and the weight condition is determined and displayed accordingly. The code demonstrates a simple way to calculate BMI and provide feedback on weight status. By understanding the code and its functionality, you can create a BMI calculator for your website or application. Don't forget to leave your feedback in the comment section. Follow front.end24 on instagram for more project ideas
index.html
style.css:
index.js:
Here is the explaination:
It declares variables btn, bmiresult, and weightcondition that will reference HTML elements in the document.
It defines a function called calculate() that will be executed when a button with the id "btn" is clicked.
Inside the calculate() function, it retrieves the values of the height and weight inputs from the HTML elements with the corresponding ids.
It calculates the BMI by dividing the weight (in kilograms) by the square of the height (in meters).
It assigns the calculated BMI value to the bmiresult element's value property, which will update the displayed result in the HTML.
It uses conditional statements to determine the weight condition based on the calculated BMI.
If the BMI is less than 18.5, it sets the innerText of the weightcondition element to "Under Weight".
If the BMI is between 18.5 and 24.9 (inclusive), it sets the innerText to "Normal Weight".
If the BMI is between 25 and 29.9 (inclusive), it sets the innerText to "OverWeight".
If the BMI is greater than or equal to 30, it sets the innerText to "Obesity".
Finally, it adds an event listener to the button element. When the button is clicked, the calculate() function will be called, triggering the BMI calculation and weight condition determination.
FINAL WORDS:
In conclusion, the provided code implements a BMI (Body Mass Index) calculator using JavaScript. The BMI is calculated based on the user's input of height and weight, and the weight condition is determined and displayed accordingly. The code demonstrates a simple way to calculate BMI and provide feedback on weight status. By understanding the code and its functionality, you can create a BMI calculator for your website or application. Don't forget to leave your feedback in the comment section. Follow front.end24 on instagram for more project ideas

Comments
Post a Comment