Created
July 24, 2016 15:01
-
-
Save kseniya292/86ec19c2fbc86435963e2acc484e16e3 to your computer and use it in GitHub Desktop.
Roman Numeral Converter Algorithm- freeCodeCamp
This file contains 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
function convertToRoman(num) { | |
var romanArray = []; | |
while (num <= 3999 && num > 0) { | |
if (num >= 1000 && num <= 3999) { | |
romanArray.push("M"); | |
num -= 1000; | |
} else if (num >= 900) { | |
romanArray.push("CM"); | |
num -=900; | |
} else if (num >= 500) { | |
romanArray.push("D"); | |
num -=500; | |
} else if (num >= 400) { | |
romanArray.push("CD"); | |
num -=400; | |
} else if (num >= 100) { | |
romanArray.push("C"); | |
num -=100; | |
} else if (num >= 90) { | |
romanArray.push("XC"); | |
num -=90; | |
} else if (num >= 50) { | |
romanArray.push("L"); | |
num -=50; | |
} else if (num >= 40) { | |
romanArray.push("XL"); | |
num -=40; | |
} else if (num >= 10) { | |
romanArray.push("X"); | |
num -=10; | |
} else if (num === 9) { | |
romanArray.push("IX"); | |
num -=9; | |
} else if (num >= 5 && num <= 8 ) { | |
romanArray.push("V"); | |
num -=5; | |
} else if (num === 4) { | |
romanArray.push("IV"); | |
num -=4; | |
} else if (num >= 1 && num <= 3 ) { | |
romanArray.push("I"); | |
num -=1; | |
} else if (num > 0 ) { | |
return num; | |
} | |
} //while | |
return romanArray.join(""); | |
} | |
convertToRoman(44); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment