Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                    <h1>Roman Numeral Converter</h1>
    <div class="card">
      <label for="number">Enter a Number:</label>
      <input id="number" type="number">
      <button type="button" id="convert-btn">Convert</button>
    </div>

    <div id="output" class="card">
      <p></p>
    </div>
    <script defer src="script.js"></script>
              
            
!

CSS

              
                :root {
  --background-color: hsl(-125 100% 10% / 0.9);
  --text-color: hsl(0 0% 100%);
  --border-color: hsl(180 50% 70%);
  --font: system-ui;
  --card-background-color: hsl(from var(--background-color) h s l / 0.7);
  --button-background-color: hsl(55 100% 60%);
  --card-border-radius-top: 5px;
  --card-border-radius-bottom: 20%;

}
body, div, h1 {
  display: flex;
  justify-content: center;
  margin-inline: auto;
  flex-direction: column;
  text-align: center;
}

body {
  background-color: var(--background-color);
  color: var(--text-color);
  font-family: var(--font);
}

.card {
  width: min(33%, 300px);
  border: 1px solid var(--border-color);
  padding: 2rem 5rem;
  background-color: var(--card-background-color);
}

.card + .card {
  border-top: none;
  border-radius: 0 0 var(--card-border-radius-top) var(--card-border-radius-top);
}

.card:not(.card + .card) {
  border-bottom: none;
  border-radius: var(--card-border-radius-bottom) var(--card-border-radius-bottom) 0 0;
}

button {
  background-color: var(--button-background-color);
}
              
            
!

JS

              
                "use strict";

const input = document.getElementById("number");
const button = document.getElementById("convert-btn");
const output = document.getElementById("output");
let romNum = ''; // used in numToRoman function, which is recursive

button.addEventListener("click", () => {
  // Clear old value for romNum
  romNum = '';
  // Check if user's input is a number between 1 and 3999
  let userNum = Number(input.value);
  if (isNaN(Number(userNum)) || input.value === '') {
    output.innerText = "Please enter a valid number";
  } else if(userNum < 1) {
    output.innerText = "Please enter a number greater than or equal to 1";
  } else if(userNum > 3999) {
    output.innerText = "Please enter a number less than or equal to 3999";
  } else {
    output.innerText = numToRoman(userNum);
  }
})

const numToRoman = (num) => {
  // use a map so we can use numbers as keys
  const conversionTable = new Map();
  conversionTable
    .set(1000, "M")
    .set(900, "CM")
    .set(500, "D")
    .set(400, "CD")
    .set(100, "C")
    .set(90, "XC")
    .set(50, "L")
    .set(40, "XL")
    .set(10, "X")
    .set(9, "IX")
    .set(5, "V")
    .set(4, "IV")
    .set(1, "I");

    for(const [key, value] of conversionTable) {
      // if num is larger than or equal to key, add the Roman symbol
      // Need to test multiple times for some numbers
      console.log(num);
      if(num / key >= 1) {
        romNum += value;
        num -= key;
        console.log(romNum, "Remainder: ", num);
        return numToRoman(num);
      }
    }
  return romNum;
}
              
            
!
999px

Console