Roman Numeral Converter Using JavaScript

Roman Numeral Converter Using JavaScript
Code Snippet:Roman Numeral Converter
Author: Aleksandar Sandro Cvetković
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 658
License: MIT
Edit Code online: View on CodePen
Read More

The Roman Numeral Converter is a handy JavaScript tool that allows you to effortlessly convert decimal numbers into Roman numerals. It works by utilizing two arrays: one containing decimal values and the other with corresponding Roman numerals.

The main functionality is encapsulated in the convertToRoman method, which takes a numeric input and returns its Roman numeral representation. This code is helpful for quickly converting numbers to Roman numerals, making it a handy tool for various applications.

How to Convert Numbers to Roman Numerals Using Javascript

1. First of all, load the Google Fonts by adding the following CDN link into the head tag of your webpage’s <head> tag. (Optional)

<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Muli&amp;display=swap'>

2. Within the HTML file, include an input field for entering decimal numbers, a button to trigger the conversion, and an area to display the Roman numeral result. For example:

<div id="container">
      <div class="panel">
        <h1>Roman Numeral Converter</h1>
        <h4>
          <span>Convert the number into a</span> <span>roman numeral</span>
        </h4>
        <input id="convert" type="text" placeholder="Type any number..." />
        <button
          onclick="convertToRoman(document.getElementById('convert').value)"
        >
          Convert
        </button>
        <div class="result-container">
          <span id="result"></span>
          <button class="btn" id="clipboard">
            <i class="far fa-clipboard"></i>
          </button>
        </div>
        <small>
          Click the "Convert" button for the result. <br />
          You can also copy Roman Numerals by clicking the icon on the right
          side of the result.
        </small>
      </div>
    </div>

3. Now, style the basic interface for the converter using the following CSS styles:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Muli", sans-serif;
  color: #fff;
  height: 100vh;
  text-align: center;
}

#container {
  /*Photo by Cristina Gottardi on Unsplash - https://unsplash.com/photos/I1Lv2yX67GI*/
  background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)),
    url("https://images.unsplash.com/photo-1525874684015-58379d421a52?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1650&q=80");
  width: 100vw;
  height: 100vh;
  background-size: cover;
  background-position: bottom center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.panel {
  background-image: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7));
  border-radius: 15px;
  padding: 30px;
  margin: 15px;
}

.panel h4,
h1 {
  margin-bottom: 20px;
  letter-spacing: 0.5px;
}

.panel h4 span:first-of-type {
  opacity: 0.5;
}

.panel span:nth-of-type(2) {
  color: #dd87ff;
}

.panel input,
.panel button {
  border: none;
  border-radius: 3px;
  display: block;
  font-size: 16px;
  padding: 8px 10px;
  margin: 15px 0;
  width: 100%;
}

.panel button {
  background-image: linear-gradient(to bottom right, #c564eb, #c85ff1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #fff;
  cursor: pointer;
}

.panel small {
  font-size: 0.75em;
  line-height: 10px;
  opacity: 0.5;
  letter-spacing: 1px;
}

.result-container {
  background-color: rgba(0, 0, 0, 0.4);
  display: flex;
  justify-content: flex-start;
  align-items: center;
  position: relative;
  font-size: 18px;
  letter-spacing: 1px;
  padding: 12px 10px;
  height: 50px;
  width: 100%;
  margin: 15px 0;
}

.result-container #result {
  word-wrap: break-word;
  max-width: calc(100% - 40px);
}

.result-container .btn {
  font-size: 20px;
  position: absolute;
  top: 5px;
  right: 5px;
  height: 40px;
  width: 40px;
  margin: 0;
}

.btn {
  border: none;
  color: #fff;
  cursor: pointer;
  font-size: 16px;
  padding: 8px 12px;
}

4. Load the Font Awesome Icons Kit by adding the following CDN link just before closing the <body> element:

 <script src='https://kit.fontawesome.com/7a230f0a67.js'></script>

5. Finally, include the following JavaScript code into your HTML file, either by linking an external file or placing it within <script> tags.

function convertToRoman(num) {
  let decimalValue = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  let romanNumeral = [
    "M",
    "CM",
    "D",
    "CD",
    "C",
    "XC",
    "L",
    "XL",
    "X",
    "IX",
    "V",
    "IV",
    "I"
  ];

  let romanized = "";

  for (let index = 0; index < decimalValue.length; index++) {
    while (decimalValue[index] <= num) {
      romanized += romanNumeral[index];
      num -= decimalValue[index];
    }
  }

  if (typeof num === "number") {
    return (document.getElementById("result").innerHTML = romanized);
  }
  return (document.getElementById("result").innerHTML =
    "Your input is not valid!");
}

document.getElementById("clipboard").addEventListener("click", copyToClipboard);

function copyToClipboard() {
  let copyText = document.getElementById("result");
  let textArea = document.createElement("textarea");

  const emptyResultField = result.innerText;
  if (!emptyResultField) {
    return;
  }

  textArea.value = copyText.textContent;
  document.body.appendChild(textArea);
  textArea.select();
  document.execCommand("Copy");
  textArea.remove();

  alert("Roman numerals copied to clipboard!");
}

That’s all! hopefully, you have successfully created a Roman Numeral Converter using JavaScript. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X