JavaScript - Decimal To Roman Numeral Converter
Submitted by razormist on Tuesday, April 23, 2019 - 17:14.
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.
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-6">
- <div class="form-group">
- <input type="number" id="number" class="form-control"/>
- <br />
- <br />
- <br />
- </div>
- </div>
- </div>
- </body>
- </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- function convert(){
- if(document.getElementById('number').value != ""){
- var number = parseInt(document.getElementById('number').value);
- if(romanConverter(number) !== 0){
- 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>";
- }else{
- document.getElementById('result').innerHTML = "";
- }
- }else{
- alert("Please enter a number first!");
- }
- }
- function reset(){
- document.getElementById('number').value = "";
- document.getElementById('result').innerHTML = "";
- }
- function romanConverter(val) {
- var roman=['I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M'];
- var numbers=[1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
- var x = parseInt(val);
- if( x<1 || x>4999 ){
- alert(x+' is invalid number.');
- return 0;
- }
- var result = "";
- for(i=12; i>=0;)
- if(numbers[i]<=x ){
- result += roman[i];
- x -= numbers[i];
- }
- else{
- i--;
- }
- return(result);
- }
Add new comment
- 200 views