Roman Numeral Converter solver with 3 loops HELP!

Roman Numeral Converter solver with 3 loops HELP!
0

#1

hi all!

so i solved the challenge with this horrible code, and i don’t know how to make it better.
also, this code can’t really convert numbers more than 3999, unless i hard coded it in both the arrays.

what am i missing please, i want to get better.

this is my code

var romanNums = ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", "M", "MM", "MMM"];
var arabic = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100", "200", "300", "400", "500", "600", "700", "800", "900", "1000", "2000", "3000"];
function convertToRoman(num) {
	var result = [] ;
	var display = "";
	//separate the number and add them into an array
	var numToArray = num.toString().split("").map(Number);
	for (var i = 0; i < numToArray.length ; i++) {
		var inte = numToArray[i];
		//keep counting of the zeros added to the integer.
		var addZero = inte;
		//add zero everytime it loops
		for (var j = 0; j < numToArray.length - i-1; j++) {
			addZero += "0";
		};
		//addZero stored in an array and transformed all into integers
		result.push(parseInt(addZero));
		for (var d = 0; d < arabic.length; d++) {
			//find the match and add the romanNumbers next to each other 
			if(result[i] == arabic[d]){
				display += romanNums[d];
			}
		}
	};
	console.log(display);
}
convertToRoman(3000);

#2

That many loops is a bit confusing, but the important thing is if your code solves the problem. Any solution is a valid solution! Don’t let refactoring slow your progress on FCC.

But since you asked… I think you’re onto the best solution with your two arrays, one of Arabic and one of Roman numerals. I used a similar method.

However… I only used one loop, and only about 17 lines of code.

I think you can simplify by getting rid of your for loops (all of them) and eliminating your addZero function. Essentially, this algorithm is a simple problem: You keep looping through a number, finding the largest roman numeral you can that divides into it. Then subtract the value of that numeral from your original number, and repeat until your original number is zero. What you have in your result array is then the roman numeral you want.

For instance, if we had the number 11 to start with:

  1. The largest roman numeral that fits there is X. So we subtract the value of X from num, and we’re left with 1. We push X into our result array.
  2. The largest numeral that can divide into 1 is I. We subtract that value and push I to our array.
  3. Our num is now 0, so the loop breaks (a while loop, obviously.)
  4. Our result array is [X,I]. We join that to get our final answer.

You can get the value of a roman numeral by grabbing its index position, and then comparing that with the value of arabic[i]

Hope that helps! One while loop, use subtraction.


#3

thank you so much, the logic you showed me is so simple i didin’t even think about it.