• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create a Simple BMI Calculator in JavaScript

MeSvAk

Digital Ad Innovator
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
134
Likes
124
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
How to Create a Simple BMI Calculator in JavaScript

Introduction

In this tutorial we will create a How to Create a Simple BMI Calculator in JavaScript. This tutorial purpose is to help you create a simple BMI calculator. This will cover all the important functionality that will display your summarize BMI result. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you want to add a working BMI calculator. I will give my best to provide you the easiest way of creating this program BMI calculator. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html list the buttons for submitting the forms. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, iniitial-scale=1"

    /

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. </

    head

    >
  7. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    href

    =

    "https://sourcecodester.com"

    class

    =

    "navbar-brand"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Create a Simple BMI Calculator</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-6"

    style

    =

    "border:1px SOLID #000; padding:2rem;"

    >
  18. <form

    >
  19. <div

    class

    =

    "form-inline"

    >
  20. <label

    >

    Weight(kg):</

    label

    >
  21. <input

    type

    =

    "text"

    class

    =

    "form-control"

    id

    =

    "weight"

    size

    =

    "10"

    >
  22. </

    div

    >
  23. <div

    class

    =

    "form-inline"

    >
  24. <label

    >

    Height(cm):</

    label

    >
  25. <input

    type

    =

    "text"

    class

    =

    "form-control"

    id

    =

    "height"

    size

    =

    "10"

    >
  26. </

    div

    >
  27. <br

    /

    >
  28. <button

    class

    =

    "btn btn-primary"

    type

    =

    "button"

    onClick

    =

    "calculateBmi()"

    >

    Calculate</

    button

    >

  29. </

    form

    >
  30. </

    div

    >
  31. <div

    class

    =

    "col-md-2"

    ></

    div

    >
  32. <div

    class

    =

    "col-md-4"

    style

    =

    "border:1px SOLID #000; padding:2rem;"

    >
  33. <div

    class

    =

    "form-group"

    >
  34. <label

    >

    BMI:</

    label

    >
  35. <input

    class

    =

    "form-control"

    type

    =

    "text"

    id

    =

    "bmi"

    size

    =

    "10"

    readonly

    =

    "readonly"

    /

    >
  36. </

    div

    >
  37. <br

    /

    >
  38. <div

    class

    =

    "form-group"

    >
  39. <label

    >

    Result:</

    label

    >
  40. <input

    class

    =

    "form-control"

    type

    =

    "text"

    id

    =

    "result"

    size

    =

    "25"

    readonly

    =

    "readonly"

    /

    >
  41. </

    div

    >
  42. <br

    /

    >
  43. <button

    class

    =

    "btn btn-success"

    type

    =

    "reset"

    >

    Reset</

    button

    >
  44. </

    div

    >
  45. </

    div

    >
  46. <script

    src

    =

    "script.js"

    ></

    script

    >
  47. </

    body

    >
  48. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will automatically calculate and give you the result for your BMI when you click the button To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function

    calculateBmi(

    )

    {
  2. var

    weight =

    document.getElementById

    (

    'weight'

    )

    .value

    ;
  3. var

    height =

    document.getElementById

    (

    'height'

    )

    .value

    ;

  4. if

    (

    weight >

    0

    &&

    height >

    0

    )

    {
  5. var

    bmi =

    weight/

    (

    height/

    100

    *

    height/

    100

    )
  6. document.getElementById

    (

    'bmi'

    )

    .value

    =

    bmi;

  7. if

    (

    bmi <

    18.5

    )

    {
  8. document.getElementById

    (

    'result'

    )

    .value

    =

    "You are too thin."

    ;
  9. }
  10. if

    (

    bmi >

    18.5

    &&

    bmi <

    24.9

    )

    {
  11. document.getElementById

    (

    'result'

    )

    .value

    =

    "You are healthy."

    ;
  12. }
  13. if

    (

    bmi >

    25

    )

    {
  14. document.getElementById

    (

    'result'

    )

    .value

    =

    "You are overweight."

    ;
  15. }
  16. }
  17. else

    {
  18. alert(

    "Please enter something first!"

    )
  19. }
  20. }

In the code provided we just created just one method called calculateBmi(), this function will calculate your bmi result after providing your physical measurement. The code uses simple formula to calculate your BMI by giving your height and weight measurement.

Output:

How%20to%20Create%20a%20Simple%20BMI%20Calculator%20in%20JavaScript%201.png

The How to Create a Simple BMI Calculator in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a Simple BMI Calculator in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,340

323,349

Top