Tell us what’s happening:
Everything is passing apart from instruction no.5 and the related no.10, and I cannot seem to find a reason why?
- When the
#number
element contains the number-1
and the#convert-btn
element is clicked, the#output
element should contain the text"Please enter a number greater than or equal to 1"
.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<link rel="stylesheet" href="styles.css">
<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>
/* file: script.js */
const input = document.getElementById("number");
const button = document.getElementById("convert-btn");
const output = document.getElementById("output");
const numeralsObj = {
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,
};
function checkInput() {
const numberOnly = input.value.match(/[0-9]+/g);
if (!numberOnly || numberOnly.length === 0) {
output.innerText = "Please enter a valid number";
return;
}
const num = parseInt(numberOnly[0]);
if (num < 1) {
output.innerText = "Please enter a number greater than or equal to 1";
return;
}
if (num > 3999) {
output.innerText = "Please enter a number less than or equal to 3999";
return;
}
const romanNumeral = accumulator(num, numeralsObj);
output.innerText = romanNumeral;
}
function accumulator(num, numeralsObj) {
let accumulatorString = "";
for (const key in numeralsObj) {
const numberValue = numeralsObj[key];
while (numberValue <= num) {
num -= numberValue;
accumulatorString += key;
}
}
return accumulatorString;
}
button.addEventListener("click", checkInput);
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0
Challenge Information:
Build a Roman Numeral Converter Project - Build a Roman Numeral Converter