Roman Numeral Converter
Problem: Explained
As part of the Javascript Algorithms And Data Structures Certification you need to complete 5 javascript programs, roman numeral converter being one of them. Up next I have quoted the explanation from freeCodeCamp's website.
Convert the given number into a roman numeral.
All roman numerals answers should be provided in upper-case.
Solution: At Work
Solution:
Solution: The Code
function convertToRoman(num) {
let roman='';
let romanNumeral = ["M", "CM", "D", “CD”, “C”, “XC”, “L”, “XL”, “X”, “IX”, “V”, “IV”, “I”];
let numbers = [1000,900,500,400,100,90,50,40,10,9,5,4,1];
for (let i = 0; i < numbers.length; i++) {
while (num >= numbers[i]) {
roman += romanNumeral[i];
num -= numbers[i];
}
}
return roman;
}
The Solution: Step-by-Step
let roman='';
Define a variable where the results of the loop will be stored.
let romanNumeral = ["M", "CM", "D", “CD”, “C”, “XC”, “L”, “XL”, “X”, “IX”, “V”, “IV”, “I”];
let numbers = [1000,900,500,400,100,90,50,40,10,9,5,4,1];
Define two arrays. One with the roman numerals and anothere with the equivalent numbers. It is important that they are from highest to lowest and in the same order of equivalence.
for (let i = 0; i < numbers.length; i++)
A for-loop
will execute the code repeatedly with different values. All according to the statements following it.
Statement 1 let i = 0
sets a variable used in the loop.
Statement 2 i < numbers.length
is used to know for how long the code will run. As long as this statement is true, the code will run again.
Statement 3 i++
means that everytime the code runs a loop it will add '1' to the initial variable 'i'. You can also use i--
to subtract '1' from 'i'.
while (num >= numbers[i])
As it reads. While the num
is greater than or equal than numbers[i]
, run the code inside the {squiggly brackets}.
At the beginning of the loop i = 0
. This means numbers[i]
equals numbers[0]
. The number inside the [brackets] equal the position in the array. Position 0
in the array numbers
is 1000
.
roman += romanNumeral[i];
This code is an abbreviation of roman = roman + romanNumeral[i]
.
num -= numbers[i];
This code is an abbreviation of num = num - numbers[i]
.
The workings behind convertToRoman(2019)
1st for-loop: i = 0
// i < numbers.length
// 0 is < 13 // while (2019 >= 1000) { roman = '' + M; 1019 = 2019 - 1000; }
inside while-loop: i = 0
while (1019 >= 1000) { MM = M + M; 19 = 1019 - 1000; }
inside while-loop: i = 0
while (19 >= 1000) // 19 is not >= 1000 // break while-loop.
2nd for-loop: i = 1
// i < numbers.length
// 1 is < 13 // while (19 >= 900) // 19 is not >= 900 // break while-loop.
3rd for-loop: i = 2
// i < numbers.length
// 2 is < 13 // while (19 >= 500) // 19 is not >= 500 // break while-loop.
4th for-loop: i = 3
// i < numbers.length
// 3 is < 13 //while (19 >= 400) // 19 is not >= 400 // break while-loop.
5th for-loop: i = 4
// i < numbers.length
// 4 is < 13 // while (19 >= 100) // 19 is not >= 100 // break while-loop.
6th for-loop: i = 5
// i < numbers.length
// 5 is < 13 // while (19 >= 90) // 19 is not >= 90 // break while-loop.
7th for-loop: i = 6
// i < numbers.length
// 6 is < 13 // while (19 >= 50) // 19 is not >= 50 // break while-loop.
8th for-loop: i = 7
// i < numbers.length
// 7 is < 13 // while (19 >= 40) // 19 is not >= 40 // break while-loop.
9th for-loop: i = 8
// i < numbers.length
// 8 is < 13 // while (19 >= 10) { MMX = MM + X; 9 = 19 - 10; }
inside while-loop: i = 8
while (9 >= 10) // 9 is not >= 10 // break while-loop.
10th for-loop: i = 9
// i < numbers.length
// 9 is < 13 // while (9 >= 9) { MMXIX = MMX + IX; 0 = 9 - 9; }
inside while-loop: i = 9
while (0 >= 9) // 0 is not >= 10 // break while-loop.
11th for-loop: i = 10
// i < numbers.length
// 10 is < 13 // while (0 >= 5) // 0 is not >= 5 // no while-loop.
12th for-loop: i = 11
// i < numbers.length
// 11 is < 13 // while (0 >= 4) // 0 is not >= 4 // no while-loop.
13th for-loop: i = 12
// i < numbers.length
// 12 is < 13 // while (0 >= 1) // 0 is not >= 1 // no while-loop.
14th for-loop: i = 13
// i < numbers.length
// 13 is not < 13 // no for-loop.
return roman;
The answer was ultimately calulated in the 10th for loop: MMXIX