Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTrevor Johnson
14,427 PointsJS Help With a For Loop
Hey there, I am having some trouble with this challenge on FCC. All I have to do is convert a number into a roman numeral. Right now I am trying to break the challenge down and do it in smaller chunks, and move my way up to be able to convert larger numbers. I am able to convert numbers 1-10, but as soon as I get above 10 my output gets funky, and I am not sure why. I would appreciate any feedback on why my loop isn't correctly outputting numbers larger than 10.
Directions:
Convert the given number into a roman numeral.
function convertToRoman(num) {
var decimalValue = [1, 4, 5, 9, 10], //40, 50, 90, 100, 400, 500, 900, 1000],
romanNumeral = ['I', 'IV', 'V', 'IX', 'X'], //'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M'],
romanized = '',
newArr = [],
reverseNum = num.toString().split('').reverse().join('');
for (i = 0; i < reverseNum.length; i ++) {
var power = Math.pow(10, i);
var newNumb = power * reverseNum[i];
var zero = 0;
for (j = 0; j < decimalValue.length; j ++) {
var subtract = 0;
if (newNumb == decimalValue[j]) {
romanized += romanNumeral[j];
} else if (reverseNum[i] === '0') {
zero += 1;
}
else if (newNumb < decimalValue[j]) {
subtract = reverseNum - decimalValue[j - 1];
romanized = romanNumeral[j - 1];
for (k = 0; k < subtract; k ++) {
romanized += romanNumeral[0];
}
break;
}
}
}
return romanized;
}
convertToRoman(2);
1 Answer
Steven Parker
231,271 PointsThis seems really complicated.
I'm not quite following the logic at first glance, but it seems like it shouldn't be so complicated. And I'm totally lost about reversing the number.
Wouldn't all you need to do be loop while subtracting the value of the largest decimalValue that will fit, while collecting the corresponding romanNumeral at the same time until the value reaches 0?
Steven Parker
231,271 PointsSteven Parker
231,271 PointsJust to prove it to myself I coded it. I don't want to spoil your challenge but it took 11 lines of code.
So what's FCC?
Trevor Johnson
14,427 PointsTrevor Johnson
14,427 PointsFCC is freecodecamp.com. It's a great site for learning JavaScript
Trevor Johnson
14,427 PointsTrevor Johnson
14,427 PointsI am reversing the number to multiply each digit by its corresponding 10s value. So if my input is 123, I want to multiply 3 * 10^0, 2 * 10^1, and 1 * 10^2 so that I can try and match a value from the roman numeral array
Steven Parker
231,271 PointsSteven Parker
231,271 PointsI still don't understand all the multiplication and exponent stuff. Maybe there is a way to do it using that, but I can tell you that the solution I coded is very simple and uses only subtraction.
Trevor Johnson
14,427 PointsTrevor Johnson
14,427 PointsIt's ok. Let's forget about my code. I could use some help because I have spent a lot of time on this and nothing has clicked.
Trevor Johnson
14,427 PointsTrevor Johnson
14,427 PointsI would appreciate it if you could elaborate just a little more on your hint in first comment. It didn't make much sense to me.
Trevor Johnson
14,427 PointsTrevor Johnson
14,427 PointsI figured it out. Let's see how you did it.
Steven Parker
231,271 PointsSteven Parker
231,271 PointsWow, good job. I'd have to spend some time with that to understand it!
Here's my mathematically simple approach:
Trevor Johnson
14,427 PointsTrevor Johnson
14,427 PointsHow does the break statement work in your code? Does it break out of the if statement and then repeat the for loop until num = 0?
Steven Parker
231,271 PointsSteven Parker
231,271 PointsThe for loop finds the next roman numeral (or pair). When found, the break statement ends the for loop so the while loop can continue the process and find the rest.