Build a Roman Numeral Converter Project - Build a Roman Numeral Converter

the code is not working

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang = "en">
  <head></head>
  <body>
    <header>freeCodeCamp</header>
    <main>
      <h1>Roman Numeral Converter</h1>
      <label for="number">Enter a Number: </label>
      <input id="number">
      <button id="convert-btn">Convert</button>
      <div id="output"></div>
    </main>
    <script src="script.js"></script>
  </body>
</html>
/* file: styles.css */

/* file: script.js */
const number = document.getElementById("number");
const convertBtn = document.getElementById("convert-btn");
const output = document.getElementById("output");

convertBtn.addEventListener('click', () => {
   if (number.value === "") {
     output.textContent = 'Please enter a valid number';
   }else if (number.value < 1) {
     output.textContent = 'Please enter a number greater than or equal to 1';
   } else if (number.value >= 4000) {
     output.textContent = 'Please enter a number less than or equal to 3999';
   }  else if (number.value < 4000) {
    output.textContent = convertToRoman(number.value);}
   else {
    return true;
   }
 })

function convertToRoman (num) {
  const ref = {
    M: 1000,
    CM: 900,
    D: 500,
    CD: 400,
    C: 100,
    XC: 90,
    L: 50,
    XL: 40,
    X: 10,
    IX: 9,
    V: 5,
    IV: 4,
    I: 1
  };

  let result = '';

  for (let key in ref) {
    while (num > ref[key]) {
      result += key;
      num -= ref[key];
    }
  }
  return result;
  
}
console.log(convertToRoman(9))

Your browser information:

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

Challenge Information:

Build a Roman Numeral Converter Project - Build a Roman Numeral Converter

Hi @iobateru

What have you done to debug?

The converter is off by roman numeral I

Arabic numeral 1 does not display anything.
Arabic numeral 2 displays II
Arabic numeral 10 displays IX

Happy coding

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