css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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-fluid">
  <div class="calculator">
    <div class="row">
      <h1 class="text-center">Roman Calculator</h1></div>
    <div class="row">
      <div class="col-sm-12">
        <input type="text" id="result" class="form-control" disabled>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-12">
        <button class="btn btn-primary raised medium">AC</button>
        <button class="btn btn-primary raised">CE</button>
        <button class="btn btn-primary raised">/</button>

      </div>
    </div>
    <!-- 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 -->
    <!-- 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' -->

    <div class="row">
      <div class="col-sm-12">
        <button class="btn btn-primary raised">C</button>
        <button class="btn btn-primary raised">D</button>
        <button class="btn btn-primary raised">M</button>
        <button class="btn btn-primary raised">+</button>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-12">
        <button class="btn btn-primary raised">V</button>
        <button class="btn btn-primary raised">X</button>
        <button class="btn btn-primary raised">L</button>
        <button class="btn btn-primary raised">-</button>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-12">
        <button class="btn btn-primary raised">I</button>
        <button class="btn btn-primary raised">II</button>
        <button class="btn btn-primary raised">III</button>
        <button class="btn btn-primary raised">*</button>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-12">
        <button class="btn btn-primary raised large">=</button>
      </div>
    </div>
  </div>
</div>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Open+Sans);
@import url(https://fonts.googleapis.com/css?family=Cinzel);
body {
  font-family: 'Open Sans', sans-serif;
  background: #9b59b6;
}

h1 {
  font-family: 'Cinzel', serif;
  padding: 0px;
  margin: 0px;
}

div.calculator {
  box-shadow: 12px 12px 0px #555555;
  padding: 35px 40px;
  width: 320px !important;
  margin: 50px auto;
  border-radius: 20px;
  background: #bdc3c7;
  border: 2px solid #BBB;
}

.btn {
  width: 50px;
  padding: 15px;
  margin: 3px;
  border: 0 none;
  font-weight: 700;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.btn:focus,
.btn:active:focus,
.btn.active:focus {
  outline: 0 none;
}

.btn-primary {
  background: #0099cc;
  color: #ffffff;
}

.btn-primary:hover,
.btn-primary:focus,
.btn-primary:active,
.btn-primary.active,
.open > .dropdown-toggle.btn-primary {
  background: #33a6cc;
}

.btn-primary:active,
.btn-primary.active {
  background: #007299;
  box-shadow: none;
}

.btn-primary.raised {
  box-shadow: 0 3px 0 0 #007299;
}

.btn-primary.raised:active,
.btn-primary.raised.active {
  background: #33a6cc;
  box-shadow: none;
  margin-bottom: -3px;
  margin-top: 3px;
}

.btn.medium {
  width: 110px;
}

.btn.large {
  width: 230px;
}

#result {
  background: #FEFEFE;
  border: 2px solid #BBB;
  margin: 10px 0;
  padding: 20px;
  font-weight: 800;
  font-size: 16px;
  text-align: right;
}
            
          
!
            
              var romanNumber = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
var romanLetter = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];

/**
 * Checks to see if a string starts with another string.
 * @param  {String} string The string to check
 * @param  {String} prefix The prefix to look for
 * @return {Boolean}       
 */
function stringStartsWith(string, prefix) {
  return string.slice(0, prefix.length) == prefix;
}

/**
 * Converts a number to a Roman Numeral
 * @param  {Integer} num The number to convert
 * @return {String}      The Roman Numeral
 */
function convertNumberToRomanString(num) {

  var ans = "";

  //find the closest roman numeral
  for (var i = 0; i < romanNumber.length; i++) {

    if (num >= romanNumber[i]) {

      // Add the roman numeral
      ans += romanLetter[i];

      // subtract from digit being converted
      num -= romanNumber[i];

      //check again (alternately use modulus)
      i--;

    }
  }

  return ans;
}

/**
 * Converts a Roman Numeral to an integer
 * @param  {String}  str The Roman Numeral to convert
 * @return {Integer}     The integer representing the Roman Numeral
 */
function convertRomanStringToNumber(str) {
  var ans = 0;

  //find the closest roman numeral
  while (str.length > 0) {
    for (var i = 0; i < romanLetter.length; i++) {

      if (stringStartsWith(str, romanLetter[i])) {
        str = str.slice(romanLetter[i].length);
        ans += romanNumber[i];
        i = 0;
      }

    }
  }

  return ans;
}

var animation = "animated pulse";

$("#result").bind("webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend",
  function() {
    $("#result").removeClass(animation);
  });

function Result(strId) {

  this.strId = strId;
  this.total = [];
  this.current = "";

  this.updateScreen = function(str) {
    //TRUNCATE at 14 digits
    $(this.strId).val(this.current.toString().substring(0, 12));

  }

  this.clearAll = function() {
    this.total = [];
    this.set("");
  }

  this.clearScreen = function() {
    this.set("");
  }

  this.set = function(val) {
    this.current = val;
    this.updateScreen();
  }

  this.append = function(val) {
    this.current += val;
    this.set(this.current);
  }

  this.operator = function(operator) {

    var num = convertRomanStringToNumber(this.current);
    this.total.push(num);

    if (operator === "=") {
      this.set(this.equals());
    } else {
      this.total.push(operator);
      this.clearScreen();
    }
  }

  this.equals = function() {
    $(strId).addClass(animation);

    var answer = eval(this.total.join(''));
    this.clearAll();
    return convertNumberToRomanString(answer);
  }

};

var result = new Result("#result");

$("button").click(function() {

  var pressed = $(this).text();

  // Clear everything
  if (pressed === "AC") {
    result.clearAll();
  }
  // Only clear screen
  else if (pressed === "CE") {
    result.clearScreen();
  }
  // If it's a character
  else if (/[A-Z]+/.test(pressed)) {
    result.append(pressed);
  }
  // If it's a command
  else {
    result.operator(pressed);
  }
});
            
          
!
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.
Loading ..................

Console