-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman Numerals.js
More file actions
47 lines (45 loc) · 1.19 KB
/
Copy pathRoman Numerals.js
File metadata and controls
47 lines (45 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*This takes a number and returns it in roman numerals
does it work 100%? probably not
could it be optimized and cleaned up? definitely
are there better places to do this? lots of them*/
function getVal(a,b,c){
for(var x=0;x<=6;x++){
if(a==b[x]||a===c[x]){
return x;
}
}
}
var num=parseInt(prompt("Enter number."));
var newNum="";
var vals=[1,5,10,50,100,500,1000];
var signs=["I","V","X","L","C","D","M"];
while(num>0){
if(num==9){
num-=9;
newNum+="IX";
}else if(num==49){
num-=49;
newNum+="XLIX";
}else if(num==99){
num-=99;
newNum+="XCIX";
}else if(num==499){
num-=499;
newNum+="CDXCIX";
}else if(num==999){
num-=999;
newNum+="CMXCIX";
}
for(var x=6;x>=0;x--){
if(vals[x]<=num){
newNum+=signs[x];
num-=vals[x];
}
}
if(newNum.length>=4&&newNum.substring(newNum.length-1,newNum.length)===newNum.substring(newNum.length-2,newNum.length-1)&&newNum.substring(newNum.length-2,newNum.length)===newNum.substring(newNum.length-4,newNum.length-2)){
n=getVal(newNum.substring(newNum.length-1,newNum.length),vals,signs);
newNum=newNum.substring(0,newNum.length-4);
newNum+=signs[n]+signs[n+1];
}
}
console.log(newNum);