JavaScript - Simple Convert Decimal to Octal

In this tutorial we will create a Simple Convert Decimal to Octal using JavaScript. This code will convert the given decimal into octal when the user click the convert button. The code use onclick() function to parse the given number as int to convert the value into octal by adding a dot notation string(8). Feel free to modify and apply it in your system, this is a user-friendly kind of program We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

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.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <meta charset="UTF-8" name="viewport" content="width=device-    width, initial-scale=1"/>
  4.         <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5.         <nav class="navbar navbar-default">
  6.                 <div class="container-fluid">
  7.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  8.                 </div>
  9.         </nav>
  10.         <div class="col-md-3"></div>
  11.         <div class="col-md-6 well">
  12.                 <h3 class="text-primary">JavaScript - Simple Convert Decimal to Octal</h3>
  13.                 <hr style="border-top:1px dotted #ccc;"/>
  14.                 <div class="col-md-3"></div>
  15.                 <div class="col-md-6">
  16.                         <div class="form-group">
  17.                                 <label>Enter a decimal</label>
  18.                                 <input type = "number" id="decimal" class="form-control"/>
  19.                         </div>
  20.                         <center><button class="btn btn-primary" onclick="decimalToOctal();">Convert</button> <button class="btn btn-success" onclick="reset();">Reset</button></center>
  21.                        
  22.                         <br><br>
  23.                         <div id = "result"></div>
  24.                 </div>
  25.         </div>
  26.  
  27. <script src="js/script.js"></script>
  28. </body>
  29. </html>

Creating the Script

This code contains the script of the application. This code will convert the decimal into octal 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.
  1. function reset(){
  2.         document.getElementById("decimal").value = "";
  3.         document.getElementById("result").innerHTML = "";
  4. }
  5.  
  6. function decimalToOctal() {
  7.         var decimal = document.getElementById("decimal").value;
  8.        
  9.         if(decimal == ""){
  10.                 alert("Please fill up the required field!");
  11.         }else{
  12.                 decimal = parseInt(decimal);
  13.                 var octal = decimal.toString(8);
  14.                 document.getElementById("result").innerHTML = "<center><label style='font-size:18px;'>The octal number is</label></center><center><h3 class='text-primary'>"+octal+"</h3></center>";
  15.         }
  16. }
There you have it we successfully created a Simple Convert Decimal to Octal 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