-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsolutionJavaScript.js
More file actions
36 lines (26 loc) · 951 Bytes
/
solutionJavaScript.js
File metadata and controls
36 lines (26 loc) · 951 Bytes
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
//Function to convert input number to roman numeral
function convertToRoman(number) {
// Create an object to compare the input number and convert it to roman numeral
let baseRomanNumerals = {
M:1000, CM:900, D:500, CD:400, C:100,
XC:90, L:50, XL:40, X:10, IX:9, V:5,
IV:4, I:1
}
// initialize the output string;
let romanConvert = '';
// Iterate through each numeral
for(let i in baseRomanNumerals){
// if input number is greater than the current roman numeral in loop
while(number >= baseRomanNumerals[i]){
// concat the roman numeral in the output string
romanConvert += i;
// decrease the input number by that numerals decimal equivalent
number -= baseRomanNumerals[i]
// repeat until number decrease below the current roman numeral
}
// Continue to the next numeral
}
// return the roman numeral
return romanConvert;
}
console.log(convertToRoman(152));