Hi I have been working on my roman numeral conveter but im kinda stuck. I fix a few things but now when I input i just get several I’s as the output not what the number should convert to so 4 brings back IIII not IV i could use some help thanks!
const number = document.getElementById("number");
const convertBtn = document.getElementById("convert-btn");
const output = document.getElementById("output");
convertBtn.addEventListener("click",valid);
function valid() {
if(number.value == "") {
output.innerText = "Please enter a valid number";
} else if (number.value >= 4000) {
output.innerText ="Please enter a number less than or equal to 3999";
} else if (number.value <= 0) {
output.innerText = "Please enter a number greater than or equal to 1";
} else {
romanNumeral()
}
}
const romanNumeral = () => {
const table = {
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,
};
for( const key in table) {
const numberValue = table[key];
let accumulator = '';
let num = number.value;
while (numberValue <= num) {
num -= numberValue;
accumulator += key;
}
output.innerText = `${accumulator}`;
}
return accumulator
}
your num value is reset to number.value for each value of key, you may want to consider properly in which order you want things to execute
With a number.value of '9' you get accumulator: 'IXVIVIVIIIIIIIII'
ok so i understand the first part but i im not really understanding the second. if i input 9 shouldent it do the for loop first than the while loop and than add it to the accumulator?