123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container text-center" id="main">

  <div class="row">
    <p class="lead" id="numToRoman">Numbers to Roman Numerals</p>
  </div>

  <div class="row">
    <input class="col-xs-offset-1 col-xs-10 col-sm-offset-3 col-sm-6 col-md-offset-3 col-md-6 col-lg-offset-3 col-lg-6 text-center" placeholder="Enter a number (maximum: 4999)" type="text" id="num"></input>
  </div>


  <div>
    <div class="row">
      <div class="col-xs-offset-1 col-xs-10 col-sm-offset-3 col-sm-6 col-md-offset-3 col-md-6 col-lg-offset-3 col-lg-6" id="romanResult">
        <p></p>
      </div>
    </div>
  </div>

  <div class="row">
    <p class="lead" id="romanToNum">Roman Numerals to Numbers</p>
  </div>

  <div class="row">
    <input class="col-xs-offset-1 col-xs-10 col-sm-offset-3 col-sm-6 col-md-offset-3 col-md-6 col-lg-offset-3 col-lg-6 text-center" placeholder="Enter a Roman numeral (e.g. XIV)" type="text" id="roman"></input>
  </div>

  <div class="row">
    <div class="col-xs-offset-1 col-xs-10 col-sm-offset-3 col-sm-6 col-md-offset-3 col-md-6 col-lg-offset-3 col-lg-6" id="romanResults">
      <p></p>
    </div>
  </div>

  <div class="row">
    <p class="lead text-center" id="instructions">Instructions</p>
  </div>

  <div class="row">
    <p class="col-xs-offset-1 col-xs-10 col-sm-offset-3 col-sm-6 col-md-offset-3 col-md-6 col-lg-offset-3 col-lg-6 text-center instructions"><u>Number to Roman numeral converter</u>:</p>
    <p class="col-xs-12 col-sm-offset-3 col-sm-6 col-md-offset-3 col-md-6 col-lg-offset-3 col-lg-6 text-center">Type a number up to 4999 into the top input area and press enter.</p>
  </div>

  <div class="row">
    <p class="col-xs-offset-1 col-xs-10 col-sm-offset-3 col-sm-6 col-md-offset-3 col-md-6 col-lg-offset-3 col-lg-6 text-center instructions"><u>Roman numeral to number converter</u>:</p> 
    <p class="col-xs-offset-1 col-xs-10 col-sm-offset-3 col-sm-6 col-md-offset-3 col-md-6 col-lg-offset-3 col-lg-6 text-center">Type a Roman numeral into the bottom input area and press enter. Incorrectly typed numerals and numerals over 4999 will result in an incorrect response.</p>
  </div>
</div>
            
          
!
            
              body{
  background: #232122;
  color: #DDDEDE;
}

#main{
  padding-top: 30px;
  padding-bottom: 30px;
}

#title{
  margin-bottom: 20px;
}

#numToRoman{
  margin-bottom: 5px;
}

#number{
  margin-bottom: 20px;
}

#romanToNum{
  margin-bottom: 5px;
}

#romanResult{
  margin-bottom: 20px;
}

input[type="text"]
{
    background: rgba(255,255,255,0.05);
    color: #DDDEDE;
    border: none;
    border-bottom: 1px solid rgba(255,255,255,0.3);
}

textarea:focus, input:focus{
    outline: none;
}

#instructions{
  margin-top: 20px;
  margin-bottom: 1px;
}

.instructions{
  margin-bottom: 1px;
}
            
          
!
            
              //PLEASE READ BELOW
//Anybody is allowed to use my code, all I ask is that you credit me if you do. Thanks so much! :)

//convert to Roman numerals
function convertToRoman(num) {
  var originalNumber = num;
  var roman = "";
  var numerals = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
  var numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  for (var i = 0; i < numbers.length; i++) {
    while (num >= numbers[i]) {
      roman += numerals[i];
      num -= numbers[i];
    }
    insertNum(originalNumber, roman)
  }
}

//convert from Roman numerals
function convertToNum(str) {
  var finalNum = 0;
  str = str.toUpperCase();  
  var originalStr = str;
  str = str.split("");
  //assign values to roman numerals
  for(var i = 0; i < str.length; i++){
    switch(str[i]){
      case "M":
        str[i] = 1000;
        break;
      case "D":
        str[i] = 500;
        break;
      case "C":
        str[i] = 100;
        break;
      case "L":
        str[i] = 50;
        break;
      case "X":
        str[i] = 10;
        break;
      case "V":
        str[i] = 5;
        break;
      case "I":
        str[i] = 1;
        break;
      default:
        str[i] = 0;
    }
  }
  
  for(var x = 0; x < str.length; x++){
    if(str[x + 1] > str[x]){
      finalNum += str[x + 1] - str[x];
      x += 1;
    }
    else{
      finalNum += str[x];
    }
  }
  var correctRoman = checkNumerals(finalNum, originalStr);
  console.log(correctRoman);
  insertRomanNum(finalNum, originalStr, correctRoman);
  
}

function checkNumerals(number, str){
  var originalNumber = number;
  var roman = "";
  var numerals = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
  var numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  for (var i = 0; i < numbers.length; i++) {
    while (number >= numbers[i]) {
      roman += numerals[i];
      number -= numbers[i];
    }
  }
  return roman;
  console.log(roman);
}

function insertNum(num, roman) {
  $("#romanResult").html(num + " = " + roman);
}

function insertRomanNum(num,roman,correctRoman){
  if(correctRoman != roman){
    $("#romanResults").html(roman + " = " + num + ". However, you used the wrong format. The correct Roman numeral format is: " + correctRoman);
  }
  else{
   $("#romanResults").html(roman + " = " + num); 
  }
}

$(document).ready(function() {
  //number to Roman numeral input

  $("#num").keyup(function(evt) {
    evt.preventDefault();
    if (evt.keyCode == 13) {
      var num = $("#num").val();
      $("#num").val("");
      if (num < 5000) {
        convertToRoman(num);
      }
      //error
      else {
        $("#romanResult").html("<p>Sorry, but " + num + " is not a valid value for this converter.");
      }

    }
  });

  $("#roman").keyup(function(evt) {
    evt.preventDefault();
    if (evt.keyCode == 13) {
      var romanNum = $("#roman").val();
      convertToNum(romanNum);
      $("#roman").val("");
    }
  });

});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console