I believe that I have properly completed the project but every time I try to submit the project the site either refreshes and nothing happens or it’ll completely crash the site. I have tried using multiple devices to submit but nothing has worked. PLEASE HELP ME!!!
Out of curiosity , can we see your code? I’ll wager that there’s an infinite loop somewhere.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Roman Numeral Converter</title>
</head>
<body>
<input id="number"></input>
<button id="convert-btn">Convert</button>
<div id="output"></div>
<script src="./script.js"></script>
</body>
</html>
const input = document.getElementById("number");
const convert = document.getElementById("convert-btn");
const output = document.getElementById("output");
const numerals = [
["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", () => {
const 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 numerals) {
while (number < value) {
result += roman;
value -+ number;
}
}
output.innertext = result;
}
})
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').
Typo?
value -+ number;
Also, value
is a const
const value = input.value;
Typo innerText
output.innertext = result;
What if number
and value
are the same?
while (number < value)
1 Like
infinite loop, value
never changes value
2 Likes
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.