JavaScript - Simple Temperature Converter

In this tutorial we will create a Simple Temperature Converter using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. This code can be used as your calculator to any mathematical problem. So Let's do the coding...

Getting started:

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.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6">
  14.                 <h3 class="text-primary">JavaScript - Simple Temperature Converter</h3>
  15.                 <hr style="border-top:1px dotted #ccc;" />
  16.                 <div class="col-md-6"> 
  17.                         <div class="alert alert-info">Fahrenheit to Celcius</div>
  18.                         <div style="border:1px solid #000; padding:20px; border-radius:10px;">
  19.                                 <div class="form-group">
  20.                                         <input type="number" id="fahrenheit1" placeholder="Enter number here" class="form-control"/>
  21.                                 </div>
  22.                                 <div class="form-group">
  23.                                         <input type="text" id="celcius1" class="form-control" readonly="readonly"/>
  24.                                 </div>
  25.                                 <center><button class="btn btn-success" onclick="convertToCe();">Convert</button></center>
  26.                         </div>
  27.                 </div>
  28.                 <div class="col-md-6">
  29.                         <div class="alert alert-info">Celcius to Fahrenheit</div>
  30.                         <div style="border:1px solid #000; padding:20px; border-radius:10px;">
  31.                                 <div class="form-group">
  32.                                         <input type="number" id="celcius2" placeholder="Enter number here" class="form-control"/>
  33.                                 </div>
  34.                                 <div class="form-group">
  35.                                         <input type="text" id="fahrenheit2" class="form-control" readonly="readonly"/>
  36.                                 </div>
  37.                                 <center><button class="btn btn-success" onclick="convertToFa();">Convert</button></center>
  38.                         </div>
  39.                 </div>
  40.         </div>
  41. <script src="js/script.js"></script>
  42. </body>
  43. </html>

Creating the Script

This code contains the script of the application. This code will automatically calculate the temperature of a person. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. function convertToCe(){
  2.         var fahrenheit1 = document.getElementById("fahrenheit1").value;
  3.         if(fahrenheit1 != ""){
  4.                 var number = parseFloat(fahrenheit1);
  5.                 document.getElementById("celcius1").value = (number - 32)/1.8 + "\xB0C";
  6.         }else{
  7.                 alert("Please enter something");
  8.         }
  9. }
  10.  
  11. function convertToFa(){
  12.         var celcius2 = document.getElementById("celcius2").value;
  13.         if(celcius2 != ""){
  14.                 var number = parseFloat(celcius2);
  15.                 document.getElementById("fahrenheit2").value = (number * 9/5) + 32 + "\xB0F";
  16.         }else{
  17.                 alert("Please enter something");
  18.         }
  19. }
There you have it we successfully created a Simple Temperature Converter 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!

Add new comment