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

              
                <div id="top-half">
  <form id="top-half-content">
    <label for="arabic-entry">Insert Arabic numeral to convert:</label>
    <input id="arabic-entry" type="text" name="arabic-entry" placeholder="Ex: 1004">
  </form>
</div>

<div id="bottom-half">
  <p id="output">Nothing's been entered...</p>
</div>
              
            
!

CSS

              
                /*Variables*/
$primary-default: #152B42;
$primary-light: #1159A6;
$primary-dark: #152B42;

$primary-default-text: white;
$primary-light-text: black;
$primary-dark-text: white;

$font-family: 'Kufam', cursive;

/*Styles*/
body {
  font-family: $font-family;
  margin: 0;
  padding: 0;
}

#top-half {
  //Layout
  position: relative;
  width: 100vw;
  height: 50vh;
  min-height: 200px;
  
  //Text
  line-height: 50vh;
  
  //Design
  color: $primary-default-text;
  background-color: $primary-default;
  
  form {
    //Layout
    position: relative;
    display: inline-block;
    width: 100vw;
    height: 50vh;
    
    //Text
    text-align: center;
    line-height: 2rem;
    
    label {
      //Layout
      position: relative;
      width: 100vw;
      
      //Text
      font-size: 30px;
      font-weight: bold;
    }
    
    input {
      //Layout
      position: relative;
      width: 80%;
      height: 8vh;
      min-height: 40px;
      padding: 10px;
      
      //Text
      text-align: center;
      
      //Design
      border-radius: 20px;
      border: 0;
      outline: none;
      box-shadow: 0 0 5px black;
    }
  }
}

#bottom-half {
  //Layout
  position: relative;
  width: 100vw;
  height: 50vh;
  min-height: 200px;
  border: 1px solid;
  
  //Text
  font-size: 20px;
  line-height: 50vh;
  text-align: center;
  
  //Design
  color: $primary-dark;
  
  p {
    //Layout
    position: relative;
    display: inline-block;
    
    //Text
    line-height: 1.5rem;
  }
}
              
            
!

JS

              
                const arabicEntry = document.querySelector("#arabic-entry");
const output      = document.querySelector("#output");

function convertToRoman(input, output) {
  //Set initial variables
  let romanArr = [];
  let num = input.value;
  
  //Check to ensure input consists solely of digits
  let numCheck = /\d/;
  num = num
    .toLowerCase()
    .split('')
    .filter(char => numCheck.test(char)).
    join('');
  
  //Ensure input field does not display invalid characters
  input.value = num;
  
  //If number is too great, alert user and bail
  if (num > 3999) {
    output.innerHTML = "Let's stick to values <4000. The <em>apostrophus</em> or <em>vinculum</em> methods are beyond the scope of this simple trinket.";
    return;
  }
  
  //Convert user's number into array that represents in the input's expanded form
  let convertNum = num  //E.g., 1004
    .toString()         //E.g., "1004"
    .split('')          //E.g., ["1", "0", "0", "4"]
    .reverse()          //E.g., ["4", "0", "0", "1"]
    .map((char, ind) => {
      return char * Math.pow(10,ind);
    })                  //E.g., [4, 0, 0, 1000]
    .reverse();         //E.g., [1000, 0, 0, 4] => Ready for conversion to Roman numerals
  
  //Simple object pairing Arabic and Roman numerals
  let numsObj = {
    "0": "",
    "1": "I",
    "2": "II",
    "3": "III",
    "4": "IV",
    "5": "V",
    "6": "VI",
    "7": "VII",
    "8": "VIII",
    "9": "IX",
    "10": "X",
    "20": "XX",
    "30": "XXX",
    "40": "XL",
    "50": "L",
    "60": "LX",
    "70": "LXX",
    "80": "LXXX",
    "90": "XC",
    "100": "C",
    "200": "CC",
    "300": "CCC",
    "400": "CD",
    "500": "D",
    "600": "DC",
    "700": "DCC",
    "800": "DCCC",
    "900": "CM",
    "1000": "M",
    "2000": "MM",
    "3000": "MMM"
  };
  
  //Convert each numeral in array to appropriate Roman counterpart
  for (let i = convertNum.length - 1; i >= 0; i--) {
    romanArr.unshift(numsObj[convertNum[i]]);
  }
  
  //Display output
  output.innerHTML = num !== "" ? romanArr.join('') : "Nothing's been entered...";
}

arabicEntry.addEventListener("input", () => { convertToRoman(arabicEntry, output); });
              
            
!
999px

Console