Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 92b08cc

Browse files
committed
Add a question
1 parent 184e83e commit 92b08cc

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

‎README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,3 +1790,74 @@ const firstNonRepeatingLetter = str => {
17901790
---
17911791

17921792
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**
1793+
1794+
## 53. Roman Numerals Encoder
1795+
1796+
Create a function that takes a positive integer less than `4,000` as its input and returns a string containing the Roman numeral representation of that integer. Modern Roman numerals are written by expressing each digit separately starting with the leftmost digit and skipping any digit with a value of zero. There can't be more than 3 identical symbols in a row. More about Roman numerals: [http://en.wikipedia.org/wiki/Roman_numerals](http://en.wikipedia.org/wiki/Roman_numerals)
1797+
1798+
Table of individual decimal places for your reference:
1799+
1800+
```markdown
1801+
Thousands Hundreds Tens Units
1802+
1 M C X I
1803+
2 MM CC XX II
1804+
3 MMM CCC XXX III
1805+
4 CD XL IV
1806+
5 D L V
1807+
6 DC LX VI
1808+
7 DCC LXX VII
1809+
8 DCCC LXXX VIII
1810+
9 CM XC IX
1811+
```
1812+
1813+
```js
1814+
const convertToRoman = number => {
1815+
// Your solution
1816+
};
1817+
1818+
console.log(convertToRoman(4)); // IV
1819+
console.log(convertToRoman(9)); // IX
1820+
console.log(convertToRoman(11)); // XI
1821+
console.log(convertToRoman(19)); // XIX
1822+
console.log(convertToRoman(22)); // XXII
1823+
console.log(convertToRoman(15)); // XV
1824+
console.log(convertToRoman(39)); // XXX + IX = XXXIX
1825+
console.log(convertToRoman(160)); // C + LX = CLX
1826+
console.log(convertToRoman(207)); // CC + VII = CCVII
1827+
console.log(convertToRoman(246)); // CC + XL + VI = CCXLVI
1828+
console.log(convertToRoman(789)); // DCC + LXXX + IX = DCCLXXXIX
1829+
console.log(convertToRoman(1009)); // M + IX = MIX
1830+
console.log(convertToRoman(1066)); // M + LX + VI = MLXVI
1831+
console.log(convertToRoman(1776)); // M + DCC + LXX + VI = MDCCLXXVI
1832+
console.log(convertToRoman(1918)); // M + CM + X + VIII = MCMXVIII
1833+
console.log(convertToRoman(1954)); // M + CM + L + IV = MCMLIV
1834+
console.log(convertToRoman(2014)); // MM + X + IV = MMXIV
1835+
console.log(convertToRoman(2421)); // MM + CD + XX + I = MMCDXXI
1836+
console.log(convertToRoman(3999)); // MMM + CM + XC + IX = MMMCMXCIX
1837+
1838+
```
1839+
1840+
<details><summary>Solution</summary>
1841+
1842+
```js
1843+
const convertToRoman = number => {
1844+
const decimals = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
1845+
const romans = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
1846+
let result = '';
1847+
1848+
decimals.map((value, index) => {
1849+
while (number >= value) {
1850+
result += romans[index];
1851+
number -= value;
1852+
}
1853+
});
1854+
1855+
return result;
1856+
};
1857+
```
1858+
1859+
</details>
1860+
1861+
---
1862+
1863+
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /