I’m running through a couple of problems in this project
When I input 0 or 4000 the error message works, but when I enter any number for example 20 I get XX and the error message with added class to the div changing it’s colors. Also when I don’t input anything and I press convert the error for that isn’t working.
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="styles.css" rel="stylesheet">
<title>Roman Numeral Converter</title>
</head>
<body>
<main>
<h1 class="title">Roman Numeral Converter</h1>
<div class="converter">
<p class="converter-text">Enter a Number:</p>
<input class="converter-input" id="number" required>
<button class="converter-btn" id="convert-btn">Convert</button>
</div>
<div class="output" id="output"></div>
</main>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background-color: #333;
}
.title{
text-align: center;
padding: 3rem;
color: white;
}
.converter{
display: flex;
flex-direction: column;
justify-content: space-evenly;
align-items: center;
border: 2px solid yellow;
border-radius: 25px;
width: 400px;
height: 250px;
margin: auto;
background-color: #555;
margin-bottom: 2.5rem;
}
.converter-text{
font-size: 1.5rem;
color: white;
font-weight: bold;
}
.converter-input{
width: 250px;
height: 40px;
background-color: #333;
border: 1px solid yellow;
color: white;
font-size: 1.5rem ;
padding: 10px;
}
.converter-btn{
width: 250px;
height: 40px;
font-size: 1.2rem;
}
.output{
width: 400px;
height: 120px;
border: 2px solid yellow;
border-radius: 25px;
margin: auto;
color: white;
font-size: 2.1rem;
display: flex;
justify-content: center;
align-items: center;
padding: 1rem;
}
.error{
border: 2px solid #a94442;
background-color: #ffadad;
color: #850000;
}
/* file: script.js */
const inputElement = document.getElementById('number');
const buttonElement = document.getElementById('convert-btn');
const outputDiv = document.getElementById('output');
function decimalToRoman(input) {
if(input <= 0){
outputDiv.classList.add('error');
return 'Please enter a number greater than or equal to 1.';
}
if(input >= 4000){
outputDiv.classList.add('error');
return 'Please enter a number less than or equal to 3999.';
}
const romanNumeralMap = [
{ value: 1000, numeral: 'M' },
{ value: 900, numeral: 'CM' },
{ value: 500, numeral: 'D' },
{ value: 400, numeral: 'CD' },
{ value: 100, numeral: 'C' },
{ value: 90, numeral: 'XC' },
{ value: 50, numeral: 'L' },
{ value: 40, numeral: 'XL' },
{ value: 10, numeral: 'X' },
{ value: 9, numeral: 'IX' },
{ value: 5, numeral: 'V' },
{ value: 4, numeral: 'IV' },
{ value: 1, numeral: 'I' }
];
for (let i = 0; i < romanNumeralMap.length; i++) {
const obj = romanNumeralMap[i];
if (input >= obj.value) {
return obj.numeral + decimalToRoman(input - obj.value);
}
}
return '';
}
buttonElement.addEventListener('click', () =>{
const num = inputElement.value;
outputDiv.innerText = decimalToRoman(num);
});
inputElement.addEventListener('keydown', (e) =>{
if(e.key === "Enter"){
const num = inputElement.value;
outputDiv.innerText = decimalToRoman(num);
}
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Challenge Information:
Build a Roman Numeral Converter Project - Build a Roman Numeral Converter