@abouthalf/

HotpinkAmbitiousModules

Nodejs

Solving the old 'roman numeral' problem with the power of unicode.

fork
loading
Files
  • index.js

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.39423310521416854","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Y29uc3QgTURDQ0xYWFZJID0gJ01EQ0NMWFhWSSc7IC8vIDE3NzYKY29uc3QgTUNNTElWID0gJ01DTUxJVic7IC8vIDE5NTQKY29uc3QgTUNNWEMgPSAnTUNNWEMnOyAvLyAxOTkwCgpmdW5jdGlvbiB0b0RlY2ltYWwobnVtZXJhbCkgewogIGNvbnN0IHRyb3VibGVzb21lR2x5cGhzID0gewogICAgIml2IjogIuKFsyIsIC8vIDQKICAgICJpeCI6ICLihbgiLCAvLyA5CiAgICAieGwiOiAiZiIsIC8vIDQwCiAgICAieGMiOiAibiIsIC8vIDkwCiAgICAiY2QiOiAiRiIsIC8vIDQwMAogICAgImNtIjogIk4iLCAvLyA5MDAKICB9OwoKICBjb25zdCBzeW0gPSB7CiAgICAiaSI6IDEsCiAgICAi4oWzIjogNCwKICAgICJ2IjogNSwKICAgICLihbgiOiA5LAogICAgIngiOiAxMCwKICAgICJsIjogNTAsCiAgICAiZiI6IDQwLAogICAgIm4iOiA5MCwKICAgICJjIjogMTAwLAogICAgIkYiOiA0MDAsCiAgICAiZCI6IDUwMCwKICAgICJOIjogOTAwLAogICAgIm0iOiAxMDAwLAogIH07CiAgbnVtZXJhbCA9IG51bWVyYWwudG9Mb3dlckNhc2UoKTsKICAoT2JqZWN0LmtleXModHJvdWJsZXNvbWVHbHlwaHMpKS5mb3JFYWNoKGsgPT4gewogICAgbnVtZXJhbCA9IG51bWVyYWwucmVwbGFjZShrLCB0cm91Ymxlc29tZUdseXBoc1trXSk7CiAgfSk7CiAgcmV0dXJuIG51bWVyYWwKICAgIC5zcGxpdCgnJykKICAgIC5tYXAobiA9PiBzeW1bbl0pCiAgICAucmVkdWNlKCh0b3RhbCwgY3VycmVudCkgPT4gdG90YWwgKyBjdXJyZW50LCAwKTsKfQoKY29uc29sZS5sb2codG9EZWNpbWFsKCdJVicpKTsKY29uc29sZS5sb2codG9EZWNpbWFsKCdWSUlJJykpOwpjb25zb2xlLmxvZyh0b0RlY2ltYWwoJ0lYJykpOwpjb25zb2xlLmxvZyh0b0RlY2ltYWwoJ1hMJykpOwpjb25zb2xlLmxvZyh0b0RlY2ltYWwoJ1hDJykpOwpjb25zb2xlLmxvZyh0b0RlY2ltYWwoTURDQ0xYWFZJKSk7CmNvbnNvbGUubG9nKHRvRGVjaW1hbChNQ01MSVYpKTsKY29uc29sZS5sb2codG9EZWNpbWFsKCdNQ01MWFhJVicpKTsKY29uc29sZS5sb2codG9EZWNpbWFsKE1DTVhDKSk7CmNvbnNvbGUubG9nKHRvRGVjaW1hbCgnTUNNWENJVicpKTs="},"asBuffer":null},"loaded":true}}
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
48
49
const MDCCLXXVI = 'MDCCLXXVI'; // 1776
const MCMLIV = 'MCMLIV'; // 1954
const MCMXC = 'MCMXC'; // 1990

function toDecimal(numeral) {
  const troublesomeGlyphs = {
    "iv": "ⅳ", // 4
    "ix": "ⅸ", // 9
    "xl": "f", // 40
    "xc": "n", // 90
    "cd": "F", // 400
    "cm": "N", // 900
  };

  const sym = {
    "i": 1,
    "ⅳ": 4,
    "v": 5,
    "ⅸ": 9,
    "x": 10,
    "l": 50,
    "f": 40,
    "n": 90,
    "c": 100,
    "F": 400,
    "d": 500,
    "N": 900,
    "m": 1000,
  };
  numeral = numeral.toLowerCase();
  (Object.keys(troublesomeGlyphs)).forEach(k => {
    numeral = numeral.replace(k, troublesomeGlyphs[k]);
  });
  return numeral
    .split('')
    .map(n => sym[n])
    .reduce((total, current) => total + current, 0);
}

console.log(toDecimal('IV'));
console.log(toDecimal('VIII'));
console.log(toDecimal('IX'));
console.log(toDecimal('XL'));
console.log(toDecimal('XC'));
console.log(toDecimal(MDCCLXXVI));
console.log(toDecimal(MCMLIV));
console.log(toDecimal('MCMLXXIV'));
console.log(toDecimal(MCMXC));
console.log(toDecimal('MCMXCIV'));
node v10.16.0