Skip to content

Commit 1d10b1b

Browse files
committed
Merge branch 'roman-numeral' of https://github.com/vinlegend1/Algorithmic_javascript into vinlegend1-roman-numeral
Vincent Hong Added code for :converting a regular number to roman number
2 parents 2a46692 + 85ca51f commit 1d10b1b

File tree

2 files changed

+116
-4
lines changed

2 files changed

+116
-4
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// a regular number
2+
const num = 3;
3+
4+
// convert a number from 0 - 3000 to a roman numeral
5+
function convertToRomanNumeral(number) {
6+
// 0, 1, 2, 3, 4, ..., 9
7+
const ones = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'];
8+
// 0, 10, 20, 30, 40, ..., 90
9+
const tens = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'];
10+
// 0, 100, 200, 300, 400, ..., 900
11+
const hundreds = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']
12+
// 0, 1000, 2000, 3000
13+
const thousands = ['', 'M', 'MM', 'MMM']
14+
15+
// get the value of digit in the thousandth postion (e.g. 3576 => 3) and accesses at index of the value of the digit (e.g. 3576 => thousands[3] = 'MMM')
16+
const thousandthDigit = thousands[Math.floor(number / 1000)];
17+
18+
// get the value of digit in the hundredth postion (e.g. 3576 => 5) and accesses at index of the value of the digit (e.g. 3576 => hundreds[5] = 'D')
19+
const hundredthDigit = hundreds[Math.floor((number % 1000) / 100)];
20+
21+
// get the value of digit in the tenth postion (e.g. 3576 => 7) and accesses at index of the value of the digit (e.g. 3576 => tens[7] = 'LXx')
22+
const tenthDigit = tens[Math.floor((number % 100) / 10)];
23+
24+
// get the value of digit in the oneth postion (e.g. 3576 => 6) and accesses at index of the value of the digit (e.g. 3576 => ones[6] = 'VI')
25+
const onethDigit = ones[Math.floor((number % 10) / 1)];
26+
27+
// combines the individual strings into one and returns...
28+
return thousandthDigit + hundredthDigit + tenthDigit + onethDigit;
29+
}
30+
31+
console.log(convertToRomanNumeral(40));
32+
33+
/*
34+
I = 1
35+
V = 5
36+
X = 10
37+
L = 50
38+
C = 100
39+
D = 500
40+
M = 1000
41+
42+
examples...
43+
II = 2
44+
III = 3
45+
IV = 4
46+
VI = 6
47+
IX = 9
48+
49+
The algorithm or the plan
50+
*/

README.md

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,15 +712,77 @@ __code Implementation:__ <p> </p>
712712
<hr>
713713
<hr>
714714
715-
<b>9. Name </b>
715+
<b>9. Numbers to Roman Numerals </b>
716716
717-
__The challenge:__ <p> </p>
717+
__The challenge:__ <p>Convert a regular number to a Roman Numeral</p>
718718
719719
720-
__Algorithmic Thinking:__ <p> </p>
720+
__Algorithmic Thinking:__ <p>You want to convert a number to a roman numeral... the trick to this problem is to kind of "split" the problem to individual digits. Instead of doing some weird convoluted solution, you should see that each digit would map to a string value and they only "add up".
721721
722+
**Example**
723+
- DXXXII = 532
724+
- Now, let's think about this example step by step. Let's take 500 out of 532. What is 500 in roman numeral? `500 = D`, right? We "append" that to our "string". now we evaluate 32... so 32 is essentially 30 + 2, so we take out 30 this time. `30 = XXX`, then append to "string". And finally, we have 2... you can't really "destructure" 2 any further so we have `2 = II`, and then append to string...
725+
- now we have `"D" + "XXX" + "II" = 532`, YAY!!!
726+
</p>
722727
723-
__code Implementation:__ <p> </p>
728+
729+
__code Implementation:__ <p>
730+
731+
```javascript
732+
// A regular number
733+
const num = 3;
734+
735+
// convert a number from 0 - 3000 to a roman numeral
736+
function convertToRomanNumeral(number) {
737+
// 0, 1, 2, 3, 4, ..., 9
738+
const ones = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'];
739+
// 0, 10, 20, 30, 40, ..., 90
740+
const tens = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'];
741+
// 0, 100, 200, 300, 400, ..., 900
742+
const hundreds = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']
743+
// 0, 1000, 2000, 3000
744+
const thousands = ['', 'M', 'MM', 'MMM']
745+
746+
// get the value of digit in the thousandth postion (e.g. 3576 => 3) and accesses at index of the value of the digit (e.g. 3576 => thousands[3] = 'MMM')
747+
const thousandthDigit = thousands[Math.floor(number / 1000)];
748+
749+
// get the value of digit in the hundredth postion (e.g. 3576 => 5) and accesses at index of the value of the digit (e.g. 3576 => hundreds[5] = 'D')
750+
const hundredthDigit = hundreds[Math.floor((number % 1000) / 100)];
751+
752+
// get the value of digit in the tenth postion (e.g. 3576 => 7) and accesses at index of the value of the digit (e.g. 3576 => tens[7] = 'LXx')
753+
const tenthDigit = tens[Math.floor((number % 100) / 10)];
754+
755+
// get the value of digit in the oneth postion (e.g. 3576 => 6) and accesses at index of the value of the digit (e.g. 3576 => ones[6] = 'VI')
756+
const onethDigit = ones[Math.floor((number % 10) / 1)];
757+
758+
// combines the individual strings into one and returns...
759+
return thousandthDigit + hundredthDigit + tenthDigit + onethDigit;
760+
}
761+
762+
console.log(convertToRomanNumeral(40));
763+
764+
/*
765+
I = 1
766+
V = 5
767+
X = 10
768+
L = 50
769+
C = 100
770+
D = 500
771+
M = 1000
772+
773+
examples...
774+
II = 2
775+
III = 3
776+
IV = 4
777+
VI = 6
778+
IX = 9
779+
780+
The algorithm or the plan
781+
*/
782+
```
783+
784+
It's very simple ones you realize it.
785+
</p>
724786
<hr>
725787
<hr>
726788

0 commit comments

Comments
 (0)