convert roman numeral to number javascript

Solutions on MaxInterview for convert roman numeral to number javascript by the best coders in the world

showing results for - "convert roman numeral to number javascript"
Giulio
26 May 2018
1function convertToRoman(num) {
2var numeral = "";
3 var arr = [
4 {number:1, roman: "I"},
5 {number:4, roman: "IV"},
6 {number:5, roman: "V"},
7 {number:9, roman: "IX"},
8 {number:10, roman: "X"},
9 {number:40, roman: "XL"},
10 {number:50, roman: "L"},
11 {number:90, roman: "XC"},
12 {number:100, roman: "C"},
13 {number:400, roman: "CD"},
14 {number:500, roman: "D"},
15 {number:900, roman: "CM"},
16 {number:1000, roman: "M"}
17 ]
18 while(num > 0){
19  var searching = arr.filter(function(myArr){
20    if(myArr.number <= num){
21      return myArr.number
22    }
23  })
24  var latest = searching.pop()
25  var full = Math.floor(num / latest.number)
26  for(let i = 0; i<full; i++){
27    numeral +=latest.roman
28  }
29  num = num % latest.number
30 }
31 return numeral 
32}
33
34console.log(convertToRoman(36));
Jonah
13 Sep 2016
1function roman_to_Int(str1) {
2if(str1 == null) return -1;
3var num = char_to_int(str1.charAt(0));
4var pre, curr;
5
6for(var i = 1; i < str1.length; i++){
7curr = char_to_int(str1.charAt(i));
8pre = char_to_int(str1.charAt(i-1));
9if(curr <= pre){
10num += curr;
11} else {
12num = num - pre*2 + curr;
13}
14}
15
16return num;
17}
18
19function char_to_int(c){
20switch (c){
21case 'I': return 1;
22case 'V': return 5;
23case 'X': return 10;
24case 'L': return 50;
25case 'C': return 100;
26case 'D': return 500;
27case 'M': return 1000;
28default: return -1;
29}
30}
31console.log(roman_to_Int('XXVI'));
32console.log(roman_to_Int('CI'));
Tao
04 Jul 2017
1class Solution {
2public:
3    int romanNumber(char n)
4    {
5        switch (n)
6        {
7        case 'I':
8                return (1);
9        case 'V':
10                return (5);
11        case 'X':
12                return (10);
13        case 'L':
14                return (50);
15        case 'C':
16                return (100);
17        case 'D':
18                return (500);
19        case 'M':
20                return (1000);
21        default:
22            return (0);
23            
24        }
25        return (0);
26    }
27    int romanToInt(string s) 
28    {
29        int len = s.size();
30        int ans = romanNumber(s[0]);
31        for(int i=1;i<len;i++)
32        {
33            int prev = romanNumber(s[i-1]);
34            int current = romanNumber(s[i]);
35            if(prev>=current)
36            {
37                ans += current;
38            }
39            else
40            {
41                ans -= prev;
42                current -= prev;
43                ans += current; 
44            }
45        }
46        return ans;
47    }
48};
queries leading to this page
roman numeral translator javascriptwrite a function to convert an integer to it e2 80 99s roman numeral equivalent javascriptconvert an integer to it e2 80 99s roman numeral equivalent javascriptconvert the given number into a roman numeral jsnumber to roman in jsare there special functions in javascript for converting numbers to roman numeralconverting numbers to roman numerals jsroman numeral to integer javascriptroman numbers to english numbers convert javascriptnumeral js from roman to numberconvert numbers to roman numerals javascriptroman numeral javascriptroman numeral to number javascriptconvert number to roman numeral jsroman numerals to numbers in jsjs convert number to roman numeralroman to number javascripthow to convert roman numerals to numbers in javascriptnumber to roman numeral javascriptroman numerals converter jshow to change numbers to roman numeral is jsroman numerals to numbers javascriptconver numeric to roman jsnumbers to roman javascriptjavascript roman numerals to integerjs roman numeral converterroman numeral converter javascriptjavascript int to romanroman to digit in javascriptconvert roman numerals to numbers jsjs number to roman numeralconvert an integer to it e2 80 99s roman numeral equivalent javascript mediumnumeral js to roman to numberroman numeral converter using javascriptroman numeral to decimal jsconvert number to roman numeral number in jsconvert number to roman numeral javascriptjavascript roman numeral converterjavascript numbers to roman numeralsroman numeral encoder jsconvert to roman numerals javascriptroman numerals to number converter javascriptconvert a number to roman numerals in jsconvert decimal to roman js number to roman numeral converter jshow to convert decimal number to roman number in javascripthow to convert number to roman numerals javascriptconvert roman numeral to integer javascriptjavascript convert roman numberals to digitsconvert roman numeral to number javascriptjavascript translate roman numeralsnum to roman in javascriptconvert a number to roman numerals jsjs int to roman roman numerals javascriptconvert roman numerals to numbers javascriptjavascript turn roman numerals into numbersroman numerals jsconvert the given number into a roman numeral javascripthow to convert a number to a roman numeral typescriptint to roman javascriptconvert numbers to roman numerals nodejshow to change the roman number jsjs roman to numberjs number to romanjavascript convert number to roman numeralsjavascript number to roman numeraljavascript convert roman numerals to numbersroman numeral converter in jsroman numbers convert javascriptgiven a roman numeral n 2c convert it into integer in jshow to convert numbers into roman numerals in javascripthow to convert numbers to roman numerals in javascriptroman numerals in javascripthow to write a roman numeral converter in jsconvert integer to roman numeral javascripthow to get roman numerals to int in jsjavascript convert number to roman numeralconvert roman numerals to numbers function javascriptroman numerical converter jsconvert decimal to roman javascripthow to convert to roman numerals javascriptconvert to roman numbers jsnumbers to roman numbers jsjavascript roman numeralsintger to roman jshow to convert decimal number to roman number in javascconvert roman to integer javascriptinteger to roman javascriptjs function convert number to roman numeralroman numerals converter javascriptto roman numbers jsroman numerals encoder javascriptconvert number to roman numerals javascriptconverting an integer into a roman numeral jsroman numeral converter jsjavascript convert roman to numeralsconvert number to roman numbers jsjavascript int to roman numeralare ther special functions in javascript for converting numbers to roman numeralconvert roman numeral to number javascript