Tell us what’s happening:
I don’t actually know what the mistakes are. My code works smoothly on my local devices, but it’s not working on freecodecamp compiler. Why is it happening and what’s the solution?
Your code so far
<!-- file: index.html -->
<input id="number" />
<button id="convert-btn">Convert</button>
<span id="output"></span>
/* file: script.js */
const convertBtn = document.getElementById("convert-btn");
const output = document.getElementById("output");
const validateNumber = (input) => {
const num = parseInt(input);
if (isNaN(num)) {
output.textContent = "Please enter a valid number";
return false;
} else if (num <= 0) {
output.textContent = "Please enter a number greater than or equal to 1";
return false;
} else if (num >= 4000) {
output.textContent = "Please enter a number less than or equal to 3999";
return false;
}
return true;
};
const convertRoman = (num) => {
if (!validateNumber(num)) return;
const arr = [
[1000, 'M'],
[900, 'CM'],
[500, 'D'],
[400, 'CD'],
[100, 'C'],
[90, 'XC'],
[50, 'L'],
[40, 'XL'],
[10, 'X'],
[9, 'IX'],
[5, 'V'],
[4, 'IV'],
[1, 'I']
];
let str = '';
let newNum = parseInt(num);
for (const item of arr) {
while (newNum >= item[0]) {
str += item[1];
newNum -= item[0];
}
}
output.textContent = str;
};
convertBtn.addEventListener("click", () => {
const input = document.getElementById("number").value;
convertRoman(input);
});
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Challenge Information:
Build a Roman Numeral Converter Project - Build a Roman Numeral Converter