JavaScript - BMI Calculator Code
Submitted by razormist on Monday, January 27, 2020 - 09:17.
In this tutorial we will create a BMI Calculator Code using JavaScript. This code will calculate a person personal bmi when user enter a specific physical information. The code use a onclick() function to call a specific code that will calculate the given value by the use of a special formula weight/(height/100*height/100) This code is free to use, you can modify it as your own,
We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .
There you have it we successfully created a BMI Calculator Code using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Getting started:
First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, iniitial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;" />
- <form>
- <div class="form-inline">
- <input type="text" class="form-control" id="weight" size="10" >
- </div>
- <div class="form-inline">
- <input type="text" class="form-control" id="height" size="10">
- </div>
- <br />
- <div class="form-inline">
- <input class="form-control" type="text" id="bmi" size="10">
- </div>
- <br />
- <div class="form-inline">
- <input class="form-control" type="text" id="result" size="25">
- </div>
- <br />
- </form>
- </div>
- </body>
- </html>
Creating the Script
This code contains the script of the application. This code will calculate a person BMI when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.- function calculateBmi() {
- var weight = document.getElementById('weight').value;
- var height = document.getElementById('height').value;
- if(weight > 0 && height > 0){
- var bmi = weight/(height/100*height/100);
- document.getElementById('bmi').value = bmi;
- if(bmi < 18.5){
- document.getElementById('result').value = "You are too thin.";
- }
- if(bmi > 18.5 && bmi < 24.9){
- document.getElementById('result').value = "You are healthy.";
- }
- if(bmi > 25){
- document.getElementById('result').value = "You are overweight.";
- }
- }
- else{
- alert("Please enter something first!")
- }
- }
Add new comment
- 2487 views