-
Notifications
You must be signed in to change notification settings - Fork 40
/
IntegerToRoman.js
32 lines (28 loc) · 913 Bytes
/
IntegerToRoman.js
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
// Source : https://oj.leetcode.com/problems/integer-to-roman/
// Author : Dean Shi
// Date : 2015-06-10
/**********************************************************************************
*
* Given an integer, convert it to a roman numeral.
*
* Input is guaranteed to be within the range from 1 to 3999.
*
**********************************************************************************/
/**
* @param {number} num
* @return {string}
*/
var intToRoman = function(num) {
var numArr = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
var romanArr = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
var result = '';
for (var i = 0; num !== 0; ++i) {
while (num >= numArr[i]) {
num -= numArr[i];
result += romanArr[i];
}
}
return result;
};
// Test case
console.log(intToRoman(999) === 'CMXCIX'); // true