Tell us what’s happening:
The issue I am having right now is that I cannot seem to complete 7, 8 or 9. Every time I try to convert a number that’s below 1000, I get the output as M apart from 1, 10 and 100.
When I console log the actual number like at the end of my code, I get the right output though.I also see an issue with console.log(num) and it shows that it minuses a 1000 even if the number inputted is less than 1000
Your code so far
<!-- 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 rel="stylesheet" href="styles.css">
<title>Roman Numeral Converter</title>
</head>
<body>
<main>
<img class="logo" src="https://cdn.freecodecamp.org/platform/universal/fcc_primary.svg" alt="freeCodeCamp Logo">
<h1>Roman Numeral Converter</h1>
<form id="form">
<fieldset>
<label>Enter a number: </label>
<input type="number" id="number" required>
<button type="button" id="convert-btn">Convert</button>
</fieldset>
</form>
<div id="output"></div>
</main>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
*{
padding:0;
margin:0;
box-sizing:border-box;
}
body{
padding:50px;
font-family:Helvetica, Arial, sans-serif;
font-size:18px;
background-color:#1b1b32;
color:#ffffff;
}
main {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo{
height:30px;
margin-bottom:20px;
background-color:#1b1b32;
}
h1{
text-align:center;
font-family:Times New Roman;
margin:20px;
max-width:300px;
}
form{
color:#ffffff;
padding:15px auto;
border:3px solid #ffffff;
background-color:#3B3B4F;
max-width:500px;
width:100%;
margin-top:20px;
}
fieldset{
border: none;
padding:25px;
margin:10px 20px;
}
label{
display:block;
font-weight:bold;
font-size:1.4rem;
margin-bottom:12px;
text-align:center;
}
input{
font-size:2.5rem;
height:60px;
width:100%;
padding:5px 10px;
margin:10px 0;
color:white;
background-color:#0A0A23;
border:1px solid #ffffff;
}
button{
cursor: pointer;
width:100%;
margin-top:12px;
padding:10px;
color:black;
background-color:#FEC043;
background-image: linear-gradient(#fecc4c, #ffac33);
border:3px solid #FEAC32;
font-size:1.5rem;
}
#output{
color:white;
background-color:#3B3B4F;
border:3px solid #ffffff;
font-size:2.5rem;
max-width:500px;
margin-top: 20px;
padding:15px auto;
text-align:center;
width:100%;
min-height: 50px;
}
/* file: script.js */
const userInput = document.getElementById("number");
const convert = document.getElementById("convert-btn");
const result = document.getElementById("output");
function convertToRoman(num) {
const numerals = {
1: 'I',
4: 'IV',
5: 'V',
9: 'IX',
10: 'X',
40: 'XL',
50: 'L',
90: 'XC',
100: 'C',
400: 'CD',
500: 'C',
900: 'CM',
1000: 'M',
};
//Stores Roman numerals after conversion
let romanizedNumerals = '';
//Creates an array with only the keys and starts from 1000
const arabicNumerals = Object.keys(numerals).reverse();
//Iterate through all the ArabicNumerals starting from 1000 and taking away from the number
arabicNumerals.forEach(key => {
while(key <= num){
romanizedNumerals += numerals[key];
num -= key;
}
console.log(num)
});
return romanizedNumerals;
}
convert.addEventListener("click",numberInput);
function numberInput(){
if(userInput.value === ''){
result.innerHTML = "Please enter a valid number";
}
else if(userInput.value < 1){
result.innerHTML = "Please enter a number greater than or equal to 1";
}
else if(userInput.value >= 4000){
result.innerHTML = "Please enter a number less than or equal to 3999"
}
else{
result.innerHTML = convertToRoman(userInput.value);
}
};
console.log(convertToRoman(9))
console.log(convertToRoman(99))
console.log(convertToRoman(232))
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