Does anyone know what I am doing wrong here?

This is for Build a roman numeral converter:

My Code so far:

function convertToRoman(num) {
  const numerals = {
    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',
  };

  let romanized = '';
  const decimalKeys = Object.keys(numerals).reverse();

  decimalKeys.forEach(key => {
    while (key <= num){
      romanized += numerals[key];
      num -= key;
    }
  });
  return romanized;
}

console.log(convertToRoman(36));

Can you tell us what’s going wrong?

Hi @jrjohnston2014

Your code passes.
Reset the step and try again. If that doesn’t work, refresh the page, disable dark mode, disable ad blockers. Or, use another browser.
If the above steps do not work, you may need to restart the computer.

Happy coding

I tried doing this, and I still get an error message.

Please post your full code and the error message.

My code so far:

function convertToRoman(num) {

const numerals = {

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’,

};

let romanized = ‘’;

const decimalKeys = Object.keys(numerals).reverse();

decimalKeys.forEach(key => {

while (key <= num){

romanized += numerals[key];

num -= key;

}

});

return romanized;

}

console.log(convertToRoman(36));

All of these tests have failed:

  • You should have an input element with an id of number.

  • Failed: You should have a button element with an id of convert-btn.

  • Failed: You should have a div element with an id of output.

  • Failed: When you click on the #convert-btn element without entering a value into the #number element, the #output element should contain the text Please enter a valid number.

  • Failed: When the #number element contains the number -1 and the #convert-btn element is clicked, the #output element should contain the text Please enter a number greater than or equal to 1

  • Failed: When the #number element contains the number 4000 or greater and the #convert-btn element is clicked, the #output element should contain the text Please enter a number less than or equal to 3999.

  • Failed: When the #number element contains the number 9 and the #convert-btn element is clicked, the #output element should contain the text IX.

  • Failed: When the #number element contains the number 16 and the #convert-btn element is clicked, the #output element should contain the text XVI.

  • Failed: When the #number element contains the number 649 and the #convert-btn element is clicked, the #output element should contain the text DCXLIX.

  • Failed: When the #number element contains the number 1023 and the #convert-btn element is clicked, the #output element should contain the text MXXIII.

Failed: When the #number element contains the number 3999 and the #convert-btn element is clicked, the #output element should contain the text MMMCMXCIX.

Also your html

Use the following method to post code to the forum:

  1. On a separate line type three back ticks.
  2. On a separate line paste your code.
  3. On the last line type three back ticks. Here is a single back tick `

Where is your DOM code?

You should have an input, a button, and an output element. The input should take in a number and convert it when the button is clicked. The result should be shown on the page using the required element.

Your code should handle missing input, too small and too large numbers (-1 and 4000).

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