const output = document.getElementById("output");
const numb = document.getElementById("number");
const conBtn = document.getElementById("convert-btn");
const romanNums = [
["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 roman = "";
conBtn.addEventListener("click", () => {
if (!numb.value) {
output.textContent = "Please enter a valid number";
} else if (numb.value <= 0) {
output.textContent = "Please enter a number greater than or equal to 1";
} else if (numb.value >= 4000) {
output.textContent = "Please enter a number less than or equal to 3999";
} else {
for (let i = 0; i < romanNums.length; i++) {
if (numb.value >= romanNums[i][1]) {
roman += romanNums[i][0]
numb.value -= romanNums[i][1]
output.textContent = roman;
i--
console.log(output.textContent)
}
}
}
});
Hello everyone!
Scratching my head over this one, tried copying my solution from the legacy version of this project but I’ve hit the wall here by one annoying little detail, which is that the textContent ADDS every conversion onto the previous one instead of clearing and giving a separate answer.
How should I go about fixing that detail or do I have to scrap my entire solution and approach it differently? Thanks in advance.
conBtn.addEventListener("click", () => {
**output.textContent = ""**
if (!numb.value) {
output.textContent = "Please enter a valid number";
} else if (numb.value <= 0) {
output.textContent = "Please enter a number greater than or equal to 1";
} else if (numb.value >= 4000) {
output.textContent = "Please enter a number less than or equal to 3999";
} else {
for (let i = 0; i < romanNums.length; i++) {
if (numb.value >= romanNums[i][1]) {
roman += romanNums[i][0]
numb.value -= romanNums[i][1]
output.textContent = roman;
i--
console.log(output.textContent)
}
}
}
});
If you mean like this then it still just appends it to the previous output. like I, II, III, IIII etc.
The roman variable was declared globally and therefore would only get reset if your whole page was refreshed. The test cases do not do that so the Roman variable would not have been reset each testcase unless it became part of the purchase click.