Skip to content

Instantly share code, notes, and snippets.

@jkwok91
Last active March 1, 2016 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkwok91/e3dd7644dc85ebbd6906 to your computer and use it in GitHub Desktop.
Save jkwok91/e3dd7644dc85ebbd6906 to your computer and use it in GitHub Desktop.
lol pbcopy. pbjelly. hehe. hehehe. heh. heh.
/*
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)));
}
@jkwok91
Copy link
Author

jkwok91 commented Nov 24, 2014

I HOPE NO ONE EVER FINDS THIS

@jkwok91
Copy link
Author

jkwok91 commented Nov 28, 2014

This is seriously the worst fucking thing ever. I can't even fucking read this. What the fuck, self. WHAT THE FUCK

@jkwok91
Copy link
Author

jkwok91 commented Mar 1, 2016

LMFAO THIS ENDLESSLY AMUSES

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment