Skip to content

Instantly share code, notes, and snippets.

@Bijesse
Created October 27, 2016 19:42
Show Gist options
  • Save Bijesse/f967162eb7323bdc7848309a139fd783 to your computer and use it in GitHub Desktop.
Save Bijesse/f967162eb7323bdc7848309a139fd783 to your computer and use it in GitHub Desktop.
Exported from Popcode. Click to import: https://popcode.org/?gist=f967162eb7323bdc7848309a139fd783
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Roman Numeral Converter</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>
<body style="background-color:##55FF99;">
<script src="https://code.jquery.com/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="jumbotron" id="titleBox">
<h1 style="text-decoration:underline;"> Roman Numerals Challenge</h1>
</div>
<div class="row">
<div class="col-sm-4 col-xs-10 col-xs-offset-1 col-sm-offset-4" id="inputBox">
<p> Translate a number <br> from 1-100! </p>
<input id="numInput" placeholder = "Number" type="text">
<button id="submit" class="btn btn-primary">Submit!</button>
</div>
</div>
<div class="row">
<div id="result" class="col-sm-4 col-xs-6 col-xs-offset-3 col-sm-offset-4">
<h1 id="rNumeral"> </h1>
</div>
</div>
</body>
</html>
// I = 1, V = 5, X = 10, L = 50, C = 100
var string;
function romanTranslate(x){
string = "";
// Seperate each place value in num
const h = Math.floor((x % 1000) / 100),
t = Math.floor((x % 100) / 10),
o = Math.floor(x % 10);
// Test place values
// console.log(h + " " + t + " " + o);
checkHundred(h);
checkTens(t);
checkOnes(o);
return string;
// console.log(string);
}
// Checks for numbers 1-9
function checkOnes(o){
// Add "X"s according to ones place value
if(o <= 3){
for(i = 0; i < o; i++){
string += "I";
}
}
else if(o === 4){
string += "IV";
}
else if(o === 5){
string += "V"
}
else if(o >= 6 && o <= 8){
string += "V";
for(i = 5; i < o; i++){
string += "I";
}
}
else if (o === 9){
string += "IX";
}
}
//checks numbers 10-90
function checkTens(t){
// Check if value is above 5 first
if(t >= 5){
string += "L";
}
// Add "X"s corresponding to tens place value
if(t === 1){
string += "X";
}
else if(t >= 2 && t <= 4){
for(i = 0; i < t; i++){
string += "X";
}
}
else if(t >= 6){
for(i = 5; i < t; i++){
string += "X";
}
}
}
function checkHundred(h){
//Add C if 100
if(h === 1){
string = "C"
}
}
// Append to doc when submit is clicked
$('#submit').click(function(){
var value = $('#numInput').val();
$('#rNumeral').text(romanTranslate(value));
});
html,body{
padding:0;
margin:0;
overflow-x:hidden;
}
body{
background:#32965D;
}
div{
text-align:center;
}
img{
position:absolute;
left:10%;
top:10%;
height:500px;
width:500px;
}
input{
display:block;
margin: 0 auto;
text-align:center;
font-size:1.5em;
width:70%;
}
p{
text-align:center;
}
#titleBox{
background-color:#FF3559;
border:black solid 5px;
border-left:none; border-right:none;
}
#inputBox,#result{
background-color:#FFF;
border:solid 5px black;
padding:5%;
font-size:1.5em;
}
#result{
margin-top:100px;
}
#submit{
margin-top: 20px;
width:50%;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment