freeCodeCamp.org

Can someone help me please. My code isn’t throwing any errors but it’s not working when it comes to the actual converting. When it comes to realizing if the number is greater than or equal to 1, less than 4000, or if the value is empty, it works. But for everything else it doesn’t. Can someone give me a clue as to what I’ve done wrong?

here’s my code:

<html>
  <head>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="styles.css" />
    <link rel="scriptsheet" href="./script.js" />
<title>Roman Numeral Converter</title>
  </head>
  <body>
<label for="number">Enter Numbers: </label>
<input id="number"></input>
<button id="convert-btn">convert</button>
<div id="output"></div>
<script src="./script.js"></script>
  </body>
</html>
<link rel="scriptsheet" href="./script.js" />
const number = document.getElementById("number");
const convertBtn = document.getElementById("convert-btn");
const output = document.getElementById("output");

const checkIfNumber = () => {
  if (number.value === ""){
    output.textContent = "Please enter a valid number";
  } else if (number.value < 0){
    output.textContent = "Please enter a number greater than or equal to 1";
  } else if (number.value > 3999){
    output.textContent = "Please enter a number less than or equal to 3999";
  }
};

const convertToRoman= (num) => {

checkIfNumber();
  const values =  
        [1000, 900, 500, 400, 100,  
         90, 50, 40, 10, 9, 5, 4, 1]; 
    const symbols =  
        ['M', 'CM', 'D', 'CD', 'C',  
         'XC', 'L', 'XL', 'X', 'IX',  
         'V', 'IV', 'I']; 
    let result = ''; 

for (let i = 0; i < values.length; i++) { 
        while (num >= values[i]) {            result += symbols[i]; 
            num -= values[i]; 
        }
}
return result;
};

function converting(){
  convertToRoman(number)
}
convertBtn.addEventListener("click",converting);

Hello,
When you are calling the function convertToRoman(number) your are passing the number variable which is a DOM element you selected at the beginning of your code, then inside the convertToRoman function definition, you are using that argument you passed (which is the number variable) as a number to compare it to other numbers which will not work

I understand what you’re saying, but can you break it down some more?

What is number being set to here?

Look at how you are using number here.

This function takes a number.

Are you passing it a number here?

I think it might help if you name your variables a little better. For example, the first line in your code, is number really being set to a number? Or is it being set to something else? I think this is causing you some confusion when you call convertToRoman(number).