JavaScript - Decimal To Roman Numeral Converter

In this tutorial we will create a Decimal To Roman Numeral Converter using JavaScript. This code will convert the given decimal value into a roman numerals when the user click the button. The code use an array to store a multiple dictionary in order to compress the index position in a situational statement, and extract the roman numeral key position to to achieve the target result. This program is a user-friendly feel free to use it in your system. We will use JavaScript to allow you to implement complex things on web pages. It enables you to create dynamically updating content and add some interactive visual for the user transactions.

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. <script type="text/javascript" language="Javascript" src="code_js.js"></script>
  7. </head>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Decimal To Roman Numeral Converter</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <div class="form-group">
  20. <label>Enter a number</label>
  21. <input type="number" id="number" class="form-control"/>
  22. <br />
  23. <center><button class="btn btn-primary" onclick="convert();">Convert to Roman Numerals</button></center>
  24. <br />
  25. <center><button class="btn btn-success" onclick="reset();">Reset</button></center>
  26. <br />
  27. <div id="result"></div>
  28. </div>
  29. </div>
  30. </div>
  31. <script src="js/script.js"></script>
  32. </body>
  33. </html>

Creating the Script

This code contains the script of the application. This code will convert the decimal to roman numerals when the button is clicked. 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 directory
  1. function convert(){
  2. if(document.getElementById('number').value != ""){
  3. var number = parseInt(document.getElementById('number').value);
  4.  
  5. if(romanConverter(number) !== 0){
  6. document.getElementById('result').innerHTML = "<center><label style='font-size:20px;'><span style='font-size:25px;' class='text-primary'>"+number+"</span> is equivalent to <span style='font-size:30px;' class='text-success'>"+romanConverter(number)+"</span></label></center>";
  7. }else{
  8. document.getElementById('result').innerHTML = "";
  9. }
  10. }else{
  11. alert("Please enter a number first!");
  12. }
  13.  
  14. }
  15.  
  16. function reset(){
  17. document.getElementById('number').value = "";
  18. document.getElementById('result').innerHTML = "";
  19. }
  20.  
  21.  
  22. function romanConverter(val) {
  23. var roman=['I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M'];
  24. var numbers=[1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
  25. var x = parseInt(val);
  26.  
  27. if( x<1 || x>4999 ){
  28. alert(x+' is invalid number.');
  29. return 0;
  30. }
  31. var result = "";
  32. for(i=12; i>=0;)
  33. if(numbers[i]<=x ){
  34. result += roman[i];
  35. x -= numbers[i];
  36. }
  37. else{
  38. i--;
  39. }
  40. return(result);
  41. }
There you have it we successfully created a Decimal To Roman Numeral 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