Last active
March 1, 2016 06:15
-
-
Save jkwok91/e3dd7644dc85ebbd6906 to your computer and use it in GitHub Desktop.
lol pbcopy. pbjelly. hehe. hehehe. heh. heh.
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
/* | |
this turns a number into a roman numeral | |
and a roman numeral into a number | |
*/ | |
var NUMBERZ = { | |
1: 'I', | |
5: 'V', | |
10: 'X', | |
50: 'L', | |
100: 'C', | |
500: 'D', | |
1000: 'M' | |
}; | |
var ROMANZ = { | |
'I': 1, | |
'V': 5, | |
'X': 10, | |
'L': 50, | |
'C': 100, | |
'D': 500, | |
'M': 1000 | |
}; | |
var jesusChrist = [], jesusChrist2 = []; // forgive me my father for i have sinned | |
for (var shit in NUMBERZ) { | |
jesusChrist.push(parseInt(shit)); | |
jesusChrist2.push(NUMBERZ[shit]); | |
} | |
jesusChrist.sort(function(a,b) { return a>b; }); // benjamin challenged me about whether or not it would auto sort, so i threw this shit in there for good measure | |
var numberOfRomanThings = jesusChrist.length; | |
function num2rom(n) { | |
// n is a number | |
// this function will return the correct roman numeral representation of the number. up to 3999, i think. | |
var num = n, rom = ''; | |
var idx = numberOfRomanThings-1; | |
for (; idx >= 0 && num > 0; idx--) { | |
var divisor = jesusChrist[idx]; | |
quotient = Math.floor(num/divisor); | |
num %= divisor; | |
// quotient can be 0-4 | |
if (quotient == 0) { continue; } | |
if (quotient < 4) { for (var i = 0; i < quotient; i++) { rom += NUMBERZ[divisor] } continue; } | |
var oneAgo = NUMBERZ[jesusChrist[idx+1]]; | |
var twoAgo = NUMBERZ[jesusChrist[idx+2]]; | |
if (rom.slice(-1) === oneAgo) { | |
rom = rom.slice(0,-1); | |
rom += NUMBERZ[divisor]+twoAgo; | |
} else { | |
rom += NUMBERZ[divisor]+oneAgo; | |
} | |
} | |
//console.log(rom); | |
return rom; | |
} | |
/* too high to prettify this right now */ | |
function whatIsTheTensPlaceOneBelowMine(someRomanNumeral) { | |
var larger = someRomanNumeral; | |
var largerVal = ROMANZ[larger]; | |
var oneFifthOfLarger = largerVal/5; //value | |
var romanNumeralValueOfOneDown = ROMANZ[jesusChrist2[jesusChrist2.indexOf(someRomanNumeral) - 1]]; | |
return oneFifthOfLarger === romanNumeralValueOfOneDown ? jesusChrist[jesusChrist2.indexOf(someRomanNumeral)-1] : jesusChrist[jesusChrist2.indexOf(someRomanNumeral)-2]; | |
} | |
function rom2num(r) { | |
// r is a roman numeral | |
var substr = r, num = 0; | |
while (substr.length > 0) { | |
var currentChar = substr[0]; | |
substr = substr.slice(1); | |
var nextChar = substr[0] || ''; | |
var currentVal = ROMANZ[currentChar]; | |
var nextVal = ROMANZ[nextChar] || 0; | |
if (nextVal > currentVal) { | |
substr = substr.slice(1); // throw both things away | |
var tenToThePowerOfTenLessThanLog10This = whatIsTheTensPlaceOneBelowMine(nextChar); | |
num += (nextVal - tenToThePowerOfTenLessThanLog10This); | |
} else { | |
num += currentVal; | |
} | |
} | |
return num | |
} | |
//tests | |
var limit = 4000; | |
var inc = 1; | |
for (var j = 0; j < limit; j+=inc) { | |
console.log(j,"=",rom2num(num2rom(j)),j===rom2num(num2rom(j))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LMFAO THIS ENDLESSLY AMUSES