JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

Tell us what’s happening:
Describe your issue in detail here.

where’s the mess
Your code so far

function convertToRoman(num) {
 const rNumerals = [1, "I", 4, "IV", 5, "V", 9, "IX", 10, "X", 40, "XL", 50, "L", 90, "XC", 100, "C", 400, "CD", 500, "D", 900, "CM", 1000, "M", 2000, "MM", 3000, "MMM", 4000, "MMMM", 5000, "V"];
 const rNumeralsTens = [10, "X", 20, "XX", 30, "XXX", 40, "XL", 50, "L", 60, "LX", 70, "LXX", 80, "LXXX", 90, "XC"] const romanNumeral = []; let v;
 while (num > 1) { if (rNumerals.includes(num)) { romanNumeral.push(rNumerals[rNumerals.indexOf(num) + 1]); break; } for (let i = 0; i < rNumerals.length; i++) {if (rNumerals[i] < num || isNaN(rNumerals[i])) { } else { v = rNumerals[i - 2]; while (num > v) {num = num - v; romanNumeral.push(rNumeralsTens.indexOf(num === 4) { romanNumeral.push("IV"); num = 0; }; if (num === 5) {romanNumeral.push("V"); num = 0; } if (num === 9) {romanNumeral.push("IX"); num === 0; } } if (num === 3 || num === 2 || num === 1) {while (num > 0) { num--;romanNumeral.push("I") } }
return romanNumeral.join("")
} convertToRoman(230);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

Link to the challenge:

It really helps if you provide a more complete question.

Talking about code is hard, but it is a critical professional skill.

The more you say, the easier it is for us to help.

This question will require thorough review.

I would try to format this code in order to make it more readable - that would help in debugging.

What’s your own opinion? Do you have any theories why this code doesn’t work as expected?

What is the output for this call:

What is the output for other testcases?

Are there any patterns in such outputs?

Probably some console.log statements should be added to start fixing the code. But again, code should be formatted before doing that.

1 Like