A Pen by Stephen Bau on CodePen.
Last active
October 5, 2018 15:25
-
-
Save bauhouse/478ab04f21fb79d4d7f22b0fa7a7fe1e to your computer and use it in GitHub Desktop.
Roman Numerals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="roman-numeral-converter" class="form-conversion"> | |
<div class="text-center mb-4"> | |
<h1 id="form-title" class="h3 mb-3 font-weight-normal">Roman Numerals</h1> | |
<p id="instructions">Enter a number and convert it to a Roman numeral.</p> | |
</div> | |
<div class="form-group"> | |
<label for="input-number">Number</label> | |
<input type="text" id="input-number" class="form-control" placeholder="Number" autofocus> | |
</div> | |
<button id="convert" class="btn btn-lg btn-primary btn-block">Convert</button> | |
<div id="roman-numeral" class="form-group"> | |
<label for="output">Roman Numeral</label> | |
<input id="output" type="text" class="form-control" autofocus> | |
<small id="output-help" class="form-text text-muted">The number converted to a Roman numeral.</small> | |
</div> | |
</div> |
A Pen by Stephen Bau on CodePen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A very elegant solution, thanks to melih on CodePen | |
// https://codepen.io/melih193/pen/Wxkqjv | |
$("button").click(function(){ | |
var num = $('#input-number').val(); | |
var roman = ''; | |
var lookup = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1}; | |
var i; | |
for ( i in lookup ) { | |
while ( num >= lookup[i] ) { | |
roman += i; | |
num -= lookup[i]; | |
console.log(i + ', ' + num + ', ' + lookup[i] + ', ' + roman); | |
} | |
} | |
$("#output").val(roman); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:root { | |
--input-padding-x: .75rem; | |
--input-padding-y: .75rem; | |
} | |
html, | |
body { | |
height: 100%; | |
} | |
body { | |
/* | |
display: -ms-flexbox; | |
display: flex; | |
-ms-flex-align: center; | |
align-items: center; | |
*/ | |
padding-top: 100px; | |
padding-bottom: 100px; | |
background-color: #f5f5f5; | |
} | |
.form-conversion { | |
width: 100%; | |
max-width: 420px; | |
padding: 15px; | |
margin: auto; | |
} | |
.form-label-group { | |
position: relative; | |
margin-bottom: 1rem; | |
} | |
.form-label-group > input, | |
.form-label-group > label { | |
padding: var(--input-padding-y) var(--input-padding-x); | |
} | |
.form-label-group > label { | |
position: absolute; | |
top: 0; | |
left: 0; | |
display: block; | |
width: 100%; | |
margin-bottom: 0; /* Override default `<label>` margin */ | |
line-height: 1.5; | |
color: #495057; | |
border: 1px solid transparent; | |
border-radius: .25rem; | |
transition: all .1s ease-in-out; | |
} | |
.form-label-group input::-webkit-input-placeholder { | |
color: transparent; | |
} | |
.form-label-group input:-ms-input-placeholder { | |
color: transparent; | |
} | |
.form-label-group input::-ms-input-placeholder { | |
color: transparent; | |
} | |
.form-label-group input::-moz-placeholder { | |
color: transparent; | |
} | |
.form-label-group input::placeholder { | |
color: transparent; | |
} | |
.form-label-group input:not(:placeholder-shown) { | |
padding-top: calc(var(--input-padding-y) + var(--input-padding-y) * (2 / 3)); | |
padding-bottom: calc(var(--input-padding-y) / 3); | |
} | |
.form-label-group input:not(:placeholder-shown) ~ label { | |
padding-top: calc(var(--input-padding-y) / 3); | |
padding-bottom: calc(var(--input-padding-y) / 3); | |
font-size: 12px; | |
color: #777; | |
} | |
#roman-numeral { | |
margin-top: 30px; | |
} | |
#convert { | |
color: #fff; | |
} | |
#convert:active { | |
background: #000; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment