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

why the IX on my code is coming out as VIII . A good example of this is MMMCMXCIX and when I enter 3999 the result is showing as MMMCMXCVIII and the question on the 7 to 11 of the project are wrong but the rest are good. seems like the

Tell us what’s happening:

the code is not working, can somebody why IX is coming out as a result of VIII for roman numeral

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang = 'en'>
  <head>
    <meta charset= "UTF">
    <meta name= "viewport" content= "width-device-width, initial-scale1.0">
    <title>Document</title>
  </head>
  <body>
    <input id="number">
    <button id="convert-btn">Convert</button>
    <div id="output"></div>
    <script src="./script.js"></script>
  </body>
  </html>

/* file: styles.css */
<html>
  <head>
    <link rel="stylesheet" src="./styles.css">
  </head>
  <body>
  <input id="number" type="number">
  <button id="convert-btn">Convert</button>
  <p id="output"></p>
  
    <script src="./script.js"></script>
  </body>
</html>
/* file: script.js */
const input = document.getElementById("number");
const convert = document.getElementById("convert-btn");
const output = document.getElementById("output");

const numeral = [
  ["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],
];

input.addEventListener("keydown", e => {
  if(e.key === "Enter") {
    convert.click()
  }
})

convert.addEventListener("click", () => {
  let value = input.value;
  if(!value){
    output.innerText = "Please enter a valid number"
  } else if(value < 0) {
    output.innerText = "Please enter a number greater than or equal to 1"
  } else if(value > 3999) {
    output.innerText = "Please enter a number less than or equal to 3999"
  } else {
    let result = "";

    for(const [roman, number] of numeral) {
      while(number < value) {
      result += roman;
      value -= number;
    }
   }
  output.innerText= result;
  } 
})

Your browser information:

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

Challenge Information:

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

Tell us what’s happening:

can somebody help, the code seems not working or maybe something is wrong on it because the 3999 which value MMMCMXCIX is coming out as a result of MMMCMXCVIII something is wrong on the code.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang = 'en'>
  <head>
    <meta charset= "UTF">
    <meta name= "viewport" content= "width-device-width, initial-scale1.0">
    <title>Document</title>
  </head>
  <body>
    <input id="number">
    <button id="convert-btn">Convert</button>
    <div id="output"></div>
    <script src="./script.js"></script>
  </body>
  </html>

/* file: styles.css */
<html>
  <head>
    <link rel="stylesheet" src="./styles.css">
  </head>
  <body>
  <input id="number" type="number">
  <button id="convert-btn">Convert</button>
  <p id="output"></p>
  
    <script src="./script.js"></script>
  </body>
</html>
/* file: script.js */
const input = document.getElementById("number");
const convert = document.getElementById("convert-btn");
const output = document.getElementById("output");

const numeral = [
  ["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],
];

input.addEventListener("keydown", e => {
  if(e.key === "Enter") {
    convert.click()
  }
})

convert.addEventListener("click", () => {
  let value = input.value;
  if(!value){
    output.innerText = "Please enter a valid number"
  } else if(value < 0) {
    output.innerText = "Please enter a number greater than or equal to 1"
  } else if(value > 3999) {
    output.innerText = "Please enter a number less than or equal to 3999"
  } else {
    let result = "";

    for(const [roman, number] of numeral) {
      while(number < value) {
      result += roman;
      value -= number;
    }
   }
  output.innerText= result;
  } 
})

Your browser information:

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

Challenge Information:

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

I went ahead and combined your posts for you. In the future, just reply to the original thread to add further updates.

Thanks.

Hi @Dniced01

  1. Have a run through how the while statement is executed.
  2. When I input 16 I see a typo.

Happy coding

I fixed the typo and still 16 is coming out as 15 in roman numeral form. I don’t know what else to do

Hi @Dniced01

You have an off by one error.
You can look up this reference online.

Happy coding