Tell us what’s happening:
Hi! I’m going crazy here haha. I’ve been watching videos on how to create algorithms but I think they were too advanced. I looked everywhere in the forum and I asked ChatGPT to guide in how to solve this. All of that was unsuccessful cause when I think I have something, I try to run the code and it says it’s wrong. Can someone please orient me? I successfully made it to far in the course and I have an attention problem, maybe that’s could be a problem for solving this things.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Roman Number Converter</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h1>
Roman Number Converter
</h1>
<label>Enter number to convert here:</label>
<input id="number"/>
<button id="convert-btn">
Convert
</button>
<span id="output"
for="number"/>
<script src="script.js"></script>
</body>
</html>
/* file: script.js */
const numberInput = document.getElementById("number");
const convertBtn = document.getElementById("convert-btn");
const result = document.getElementById("output");
convertBtn.addEventListener("click", checkUserInput);
numberInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
checkUserInput();}
});
const checkUserInput = () => {
const number = parseInt(numberInput.value);
if (isNaN(number)) {
result.textContent = "Please enter a valid number";
} else if (number < 1) {
result.textContent = "Please enter a number greater than or equal to 1";
} else if (number >= 4000) {
result.textContent = "Please enter a number less than or equal to 3999";
} else {
result.textContent = arabicToRoman(number);
}
return number.value;
};
const arabicToRoman = (checkUserInput) => {
const values = [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'];
let result = '';
for (let i = 0; i < values.length; i++) {
while (num >= values[i]) {
result += romans[i];
num -= values[i];
}
}
return result;
};
/* file: styles.css */
:root {
--light-grey: #f5f6f7;
--dark-blue: #1b1b32;
--orange: #f1be32;
}
body {
display: flex;
flex-direction: column;
align-items: center;
font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console,
monospace;
font-size: 1.125rem;
color: var(--light-grey);
background-color: var(--dark-blue);
padding: 0 4px;
}
h1 {
font-size: 2.125rem;
text-align: center;
margin: 15px 0;
}
input {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 70%;
padding: 6px;
margin: 10px;
}
label {
white-space: nowrap;
word-spacing: -6px;
margin: 3px;
}
#convert-btn {
font-size: inherit;
font-family: inherit;
background-color: var(--orange);
width: 60%;
height: 2rem;
padding: 3px;
border: 2px outset orange;
border-radius: 3px;
cursor: pointer;
}
span {
margin-inline: auto;
width: clamp(320px, 50vw, 460px);
}
#output {
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
text-align: center;
min-height: 70px;
margin-block-start: 30px;
padding: 10px;
border: 5px double var(--orange);
border-radius: 1px;
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15
Challenge Information:
Build a Roman Numeral Converter Project - Build a Roman Numeral Converter