@abouthalf/

RequiredHighNetframework

Nodejs

The old 'turn an ascii roman numeral into a decimal' trick

fork
loading
Files
  • index.js

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.6246258450510518","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Y29uc3QgTURDQ0xYWFZJID0gJ01EQ0NMWFhWSSc7IC8vIDE3NzYKY29uc3QgTUNNTElWID0gJ01DTUxJVic7IC8vIDE5NTQKY29uc3QgTUNNWEMgPSAnTUNNWEMnOyAvLyAxOTkwCgpmdW5jdGlvbiB0b0RlY2ltYWwobnVtZXJhbCkgewogIGNvbnN0IHN5bSA9IHsKICAgIGk6IDEsCiAgICB2OiA1LAogICAgeDogMTAsCiAgICBsOiA1MCwKICAgIGM6IDEwMCwKICAgIGQ6IDUwMCwKICAgIG06IDEwMDAKICB9OwogIHJldHVybiAobnVtZXJhbC50b1N0cmluZygpLnRvTG93ZXJDYXNlKCkuc3BsaXQoJycpKQogICAgLm1hcChqID0+IHN5bVtqXSkKICAgIC5yZXZlcnNlKCkgIAogICAgLnJlZHVjZSgodG90YWwsIGN1cnJlbnQsIGksIGFycikgPT4gewogICAgICByZXR1cm4gKGkgPT09IDAgfHwgY3VycmVudCA+PSBhcnJbaS0xXSkKICAgICAgICA/IHRvdGFsICsgY3VycmVudCAKICAgICAgICA6IHRvdGFsIC0gY3VycmVudDsKICAgIH0sIDApOwp9Cgpjb25zb2xlLmxvZyh0b0RlY2ltYWwoJ0lWJykpOwpjb25zb2xlLmxvZyh0b0RlY2ltYWwoJ1ZJSUknKSk7CmNvbnNvbGUubG9nKHRvRGVjaW1hbCgnSVgnKSk7CmNvbnNvbGUubG9nKHRvRGVjaW1hbCgnWEwnKSk7CmNvbnNvbGUubG9nKHRvRGVjaW1hbCgnWEMnKSk7CmNvbnNvbGUubG9nKHRvRGVjaW1hbChNRENDTFhYVkkpKTsKY29uc29sZS5sb2codG9EZWNpbWFsKE1DTUxJVikpOwpjb25zb2xlLmxvZyh0b0RlY2ltYWwoJ01DTUxYWElWJykpOwpjb25zb2xlLmxvZyh0b0RlY2ltYWwoTUNNWEMpKTsKY29uc29sZS5sb2codG9EZWNpbWFsKCdNQ01YQ0lWJykpOwo="},"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
const MDCCLXXVI = 'MDCCLXXVI'; // 1776
const MCMLIV = 'MCMLIV'; // 1954
const MCMXC = 'MCMXC'; // 1990

function toDecimal(numeral) {
  const sym = {
    i: 1,
    v: 5,
    x: 10,
    l: 50,
    c: 100,
    d: 500,
    m: 1000
  };
  return (numeral.toString().toLowerCase().split(''))
    .map(j => sym[j])
    .reverse()  
    .reduce((total, current, i, arr) => {
      return (i === 0 || current >= arr[i-1])
        ? 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