My test cases 5,6,7 and 13(last) is passed even though the output is correct. I checked the console for the error and here it is :
[TypeError: Cannot read properties of null (reading ‘innerText’)]
[TypeError: Cannot read properties of null (reading ‘innerText’)]
[TypeError: Cannot read properties of null (reading ‘innerText’)]
[TypeError: Cannot read properties of null (reading ‘innerText’)]
Heres my JavaScript Code:
const number = document.getElementById("number");
const button = document.getElementById("convert-btn");
const output = document.getElementById("output");
let result = "";
const numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
const romans = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
const convertToRoman = (num) => {
for (let i = 0; i < numbers.length; i++) {
while (num - numbers[i] >= 0) {
result = result + romans[i];
num -= numbers[i];
}
if (num === 0) {
break;
}
}
return result;
}
const convert = () => {
const intNum = parseInt(number.value);
if(number.value==="")
{
output.classList.remove("hidden");
output.setAttribute("id", "error");
output.innerText="Please enter a valid number"
}
else if (intNum <= 0 || intNum > 3999) {
output.classList.remove("hidden");
output.setAttribute("id", "error");
output.innerText = `Please enter a number ${intNum <= 0 ? "greater" : "less"} than or equal to ${intNum <= 0 ? "1" : "3999"}.`
} else {
output.classList.remove("hidden");
output.setAttribute("id", "output");
output.innerText = `${convertToRoman(intNum)}`;
}
result = "";
};
button.addEventListener("click", convert);
number.addEventListener("keydown", (e) => {
if (e.key === "Enter")
convert();
});
My HTML code is :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Roman Number Converter</title>
</head>
<body>
<main>
<div id="converterBox">
<h1>ROMAN NUMERAL CONVERTER</h1>
<div id="converter">
<label for="number"><strong>Enter a Number:</strong></label>
<input type="number" id="number" required>
<button type="submit" id="convert-btn">Convert</button>
</div>
<div id="output" class="hidden"></div>
</div>
</main>
<script src="script.js"></script>
</body>
</html>
My CSS code is :
body{
background-color: rgb(27, 27, 50);
color: #ffffff;
}
h1{
margin: 100px 0px 40px 0px;
text-align: center;
width: 29vw;
}
#converterBox{
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
flex-wrap: wrap;
}
#converter{
width: min(100vw, 400px);
font-size: 1.8rem;
min-width: 100px;
display: flex;
align-items: center;
align-content: center;
justify-content: center;
flex-direction: column;
flex-wrap: wrap;
row-gap: 20px;
background-color: #3b3b4f;
border: 4px solid #ffffff;
padding: 30px 40px;
margin-bottom: 40px;
}
input{
width: 100%;
height: 5.5vh;
background-color: #0a0a23;
border: 1px solid #ffffff;
color: #ffffff;
font-size: larger;
padding: 2px;
}
button{
width: 100%;
height: 5.5vh;
background-color: #fec546;
border: 4px solid #ffb618;
padding: 2px;
font-size: 20px;
cursor: pointer;
}
#output{
width: min(100vw, 400px);
font-size: 2.2rem;
min-width: 100px;
background-color: #3b3b4f;
border: 4px solid #ffffff;
padding: 20px 40px;
text-align: center;
}
.hidden{
display: none;
}
#error{
width: min(100vw, 400px);
font-size: 2rem;
min-width: 100px;
background-color: #ffadad;
border: 4px solid #850000;
color: #850000;
padding: 20px 40px;
text-align: center;
}