How to Reverse a Number in JavaScript

How to Reverse a Number in JavaScript


In this tutorial we will create a How to Reverse a Number in JavaScript. This tutorial purpose is to teach you how to reverse a number. This will cover all the basic function that will the number you have been entered. 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 application if you want to reverse ay numbers. I will give my best to provide you the easiest way of creating this program Reverse number. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface to our application. This code will only display the textbox and buttons in the form. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <head>
  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. </head>
  6. <nav class="navbar navbar-default">
  7. <div class="container-fluid">
  8. <a class="navbar-brand" href="">Sourcecodester</a>
  9. </div>
  10. </nav>
  11. <div class="col-md-3"></div>
  12. <div class="col-md-6 well">
  13. <h3 class="text-primary">How to Reverse a Number in JavaScript</h3>
  14. <hr style="border-top:1px dotted #ccc;"/>
  15. <div class="col-md-4">
  16. <div class="form-group">
  17. <label>Enter a number</label>
  18. <input type="number" min="0" class="form-control" id="digit"/>
  19. <br />
  20. <center><button class="btn btn-primary" onclick="reverseNumber();">Reverse</button> <button class="btn btn-success" onclick="clearField();">Clear</button></center>
  21. </div>
  22. </div>
  23. <div class="col-md-4">
  24. <div id="result"></div>
  25. </div>
  26. <div class="col-md-6">
  27. </div>
  28. </div>
  29. <script src="script.js"></script>
  30. </body>
  31. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will reverse the entered number when submitted. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function clearField(){
  2. document.getElementById("digit").value = "";
  3. document.getElementById('digit').removeAttribute('readonly');
  4. document.getElementById("result").innerHTML = "";
  5. }
  7. function reverseNumber(){
  8. let reverse=0;
  9. let input = document.getElementById("digit").value;
  10. if(input != 0){
  11. let digit = parseInt(input);
  12. while(digit){
  13. reverse = parseInt(reverse * 10);
  14. reverse = parseInt(reverse + (digit%10));
  15. digit = parseInt(digit/10);
  16. }
  17. document.getElementById("digit").setAttribute('readonly', 'readonly');
  18. document.getElementById("result").innerHTML = "<h3>RESULT</h3><center><h2 style='color:green;'>"+reverse+"</h2></center>";
  19. }else{
  20. alert("Please complete the required field!");
  21. }
  23. }

In the code above we create a method called reverseNumber(), this function will reverse the number using the parseInt() function with several logic, and set the result into variable to make it easier to display in the html page. And last method is called clearField(), this function only job is to clear any entered value in the textbox


The How to Reverse a Number 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 Reverse a Number 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

Add new comment