# Roman Numeral Converter efficiency

I recently finished the Roman Numeral Converter in the JavaScript Algorithms and Data Structures Projects challenges and I was wondering if this could be simplified. After viewing other peoples solutions I knew my solution was extremely long winded. I could have used other methods but when thinking of early solutions I only thought as far as either using switch statements, else if statements or an object oriented solution.
My solution worked but I noticed even the freecodecamp website took while to run all lines of code.
Please let me know what you think and how I can be more efficient in the future; I’ve posted my solution below. (also this is my first post on the forum, not sure if I formatted it correctly and thanks for feedback in advance!)

``````function convertToRoman(num) {
//if num / 1000 >= 1 add m
//else get remainder and follow if procedures
let romanNum = ""
let remainder = ""
//if number is > 1000
if (num / 1000 >= 1) {
for (let i = 0; i < Math.floor(num / 1000); i++) {
romanNum += "M"
}
remainder = num % 1000
// determines numeral for 100's position
if (remainder / 900 >= 1) {
romanNum += "CM"
remainder = remainder % 900
}
else if (remainder / 500 >= 1 && remainder / 500 < 2) {
romanNum += 'D'
remainder = remainder % 500
if (remainder / 100 >= 1 && remainder / 100 < 4) {
for (let i = 0; i < Math.floor(remainder / 100); i++) {
romanNum += "C"
}
remainder = remainder % 100
console.log(remainder)
}
}
else if (remainder / 400 >= 1 && remainder / 400 < 1.25) {
romanNum += 'CD'
remainder = remainder % 400
}
else if (remainder / 100 >= 1 && remainder / 100 < 4) {
for (let i = 0; i < Math.floor(remainder / 100); i++) {
romanNum += "C"
}
remainder = remainder % 100
}
// determines numeral for 10 position
if (remainder / 90 >= 1 && remainder / 90 < 100 / 90) {
romanNum += "XC"
remainder = remainder % 90
}
else if (remainder / 50 >= 1 && remainder / 50 < 1.8) {
romanNum += "L"
remainder = remainder % 50
if (remainder / 10 >= 1 && remainder / 10 < 4) {
for (let i = 0; i < Math.floor(remainder / 10); i++) {
romanNum += "X"
}
remainder = remainder % 10
}
}
else if (remainder / 40 >= 1 && remainder / 40 < 1.25) {
romanNum += "XL"
remainder = remainder % 40
}
else if (remainder / 10 >= 1 && remainder / 10 < 4) {
for (let i = 0; i < Math.floor(remainder / 10); i++) {
romanNum += "X"
}
remainder = remainder % 10
}
//determins numerals for 1's positions
if (remainder / 9 >= 1 && remainder / 9 < 10 / 9) {
romanNum += "IX"
}
else if (remainder / 5 >= 1 && remainder / 5 < 1.8) {
romanNum += "V"
remainder = remainder % 5
//determins remainder after 5
if (remainder / 1 >= 1 && remainder / 1 < 4) {
for (let i = 0; i < Math.floor(remainder / 1); i++) {
romanNum += "I"
}
}
}
else if (remainder / 4 >= 1 && remainder / 4 < 1.25) {
romanNum += "IV"
}
else if (remainder / 1 >= 1 && remainder / 1 < 4) {
for (let i = 0; i < Math.floor(remainder / 1); i++) {
romanNum += "I"
}
}

}
else if (num / 100 >= 1 && num / 100 < 10) {
remainder = num % 1000
// determines numeral for 100's position
if (remainder / 900 >= 1) {
romanNum += "CM"
remainder = remainder % 900
}
else if (remainder / 500 >= 1 && remainder / 500 < 2) {
romanNum += 'D'
remainder = remainder % 500
if (remainder / 100 >= 1 && remainder / 100 < 4) {
for (let i = 0; i < Math.floor(remainder / 100); i++) {
romanNum += "C"
}
remainder = remainder % 100
console.log(remainder)
}
}
else if (remainder / 400 >= 1 && remainder / 400 < 1.25) {
romanNum += 'CD'
remainder = remainder % 400
}
else if (remainder / 100 >= 1 && remainder / 100 < 4) {
for (let i = 0; i < Math.floor(remainder / 100); i++) {
romanNum += "C"
}
remainder = remainder % 100
}
// determines numeral for 10 position
if (remainder / 90 >= 1 && remainder / 90 < 100 / 90) {
romanNum += "XC"
remainder = remainder % 90
}
else if (remainder / 50 >= 1 && remainder / 50 < 1.8) {
romanNum += "L"
remainder = remainder % 50
if (remainder / 10 >= 1 && remainder / 10 < 4) {
for (let i = 0; i < Math.floor(remainder / 10); i++) {
romanNum += "X"
}
remainder = remainder % 10
}
}
else if (remainder / 40 >= 1 && remainder / 40 < 1.25) {
romanNum += "XL"
remainder = remainder % 40
}
else if (remainder / 10 >= 1 && remainder / 10 < 4) {
for (let i = 0; i < Math.floor(remainder / 10); i++) {
romanNum += "X"
}
remainder = remainder % 10
}
//determins numerals for 1's positions
if (remainder / 9 >= 1 && remainder / 9 < 10 / 9) {
romanNum += "IX"
}
else if (remainder / 5 >= 1 && remainder / 5 < 1.8) {
romanNum += "V"
remainder = remainder % 5
//determins remainder after 5
if (remainder / 1 >= 1 && remainder / 1 < 4) {
for (let i = 0; i < Math.floor(remainder / 1); i++) {
romanNum += "I"
}
}
}
else if (remainder / 4 >= 1 && remainder / 4 < 1.25) {
romanNum += "IV"
}
else if (remainder / 1 >= 1 && remainder / 1 < 4) {
for (let i = 0; i < Math.floor(remainder / 1); i++) {
romanNum += "I"
}
}
}
else if (num / 10 >= 1 && num / 10 < 10) {
remainder = num % 100
if (remainder / 90 >= 1 && remainder / 90 < 100 / 90) {
romanNum += "XC"
remainder = remainder % 90
}
else if (remainder / 50 >= 1 && remainder / 50 < 1.8) {
romanNum += "L"
remainder = remainder % 50
if (remainder / 10 >= 1 && remainder / 10 < 4) {
for (let i = 0; i < Math.floor(remainder / 10); i++) {
romanNum += "X"
}
remainder = remainder % 10
}
}
else if (remainder / 40 >= 1 && remainder / 40 < 1.25) {
romanNum += "XL"
remainder = remainder % 40
}
else if (remainder / 10 >= 1 && remainder / 10 < 4) {
for (let i = 0; i < Math.floor(remainder / 10); i++) {
romanNum += "X"
}
remainder = remainder % 10
}
//determins numerals for 1's positions
if (remainder / 9 >= 1 && remainder / 9 < 10 / 9) {
romanNum += "IX"
}
else if (remainder / 5 >= 1 && remainder / 5 < 1.8) {
romanNum += "V"
remainder = remainder % 5
//determins remainder after 5
if (remainder / 1 >= 1 && remainder / 1 < 4) {
for (let i = 0; i < Math.floor(remainder / 1); i++) {
romanNum += "I"
}
}
}
else if (remainder / 4 >= 1 && remainder / 4 < 1.25) {
romanNum += "IV"
}
else if (remainder / 1 >= 1 && remainder / 1 < 4) {
for (let i = 0; i < Math.floor(remainder / 1); i++) {
romanNum += "I"
}
}
}
else if (num / 1 >= 1 && num / 1 < 10) {
remainder = num % 10
//determins numerals for 1's positions
if (remainder / 9 >= 1 && remainder / 9 < 10 / 9) {
romanNum += "IX"
}
else if (remainder / 5 >= 1 && remainder / 5 < 1.8) {
romanNum += "V"
remainder = remainder % 5
//determins remainder after 5
if (remainder / 1 >= 1 && remainder / 1 < 4) {
for (let i = 0; i < Math.floor(remainder / 1); i++) {
romanNum += "I"
}
}
}
else if (remainder / 4 >= 1 && remainder / 4 < 1.25) {
romanNum += "IV"
}
else if (remainder / 1 >= 1 && remainder / 1 < 4) {
for (let i = 0; i < Math.floor(remainder / 1); i++) {
romanNum += "I"
}
}
}
return romanNum;
}

convertToRoman(36);
console.log(convertToRoman(1700))
``````

Notice that there are parts that are repeated in various parts of code after going into seemingly different `if` branches. This means while there might be some parts that need some different operations, depending on the `if` condition, the repeated parts are not it and they could be extracted from the `if`. To cut both on the code length and complexity.

In a simplified example:

``````let valueA = 0;
let valueB = 0;
if (value > 5) {
valueA += 5;
valueB += 10;
} else {
valueB += 10;
}
``````

This could be written as:

``````let valueA = 0;
let valueB = 0;
if (value > 5) {
valueA += 5;
}
valueB += 10;
``````

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.