I completed the JavaScript Algorithms and Data Structures course last month, and I’m working through the Front End course at the moment.
As I’ve been learning, I’m trying different ‘play-around’ projects to try out different JS features and put code into workable apps.
So I thought I’d take the roman numeral calculation function that I wrote for the JS Algorithms and Data Structures course and make an interactive webpage for it.
What do I need to do to make the output of the function display somewhere on the page, though?
Thanks in advance, if anyone can provide some advice. Here’s the code so far:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link href="style.css" rel="stylesheet" type="text/css" />
<title>Roman Numeral Calculator Webpage</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous" />
<script src="https://code.jquery.com/jquery-3.7.1.min.js"
integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
</head>
<body>
<div class="container-fluid main_container">
<div class="row" style="background-color: goldenrod; ">
<div class="col-md-4 col-md-offset-4">
<div class="jumbotron">
<h1 class="display-4" style="text-align: center;">Roman Numeral Calculator</h1>
<p class="lead" style="text-align: center;">To convert a number to its Roman numeral, please enter in the box
below</p>
<hr class="my-4">
<p>Only decimal numerals will be converted to Roman numerals</p>
<form class="form-inline">
<input type="text" class="form-control" id="decimal_numeral" placeholder="Enter Numeral" />
<button type="button" class="btn btn-primary" onclick="convertToRoman()">Convert</button>
<div>
<p id="roman_numeral">
<script src="script.js">
let num = document.getElementById("decimal_numeral").value;
// convertToRoman(num);
document.getElementById("roman_numeral").innerHTML = convertToRoman(num);
</script>
</p>
</div>
</div>
</body>
</html>
function convertToRoman(num) {
const romanNumerals = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
const arabicNumerals = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
let correspondingNumeral = [];
for (let i = 0; i < 13; i++) {
while (num >= arabicNumerals[i]) {
correspondingNumeral.push(romanNumerals[i])
num -= arabicNumerals[i];
}
}
let romnum = correspondingNumeral.join("");
return romnum;
}
html {
height: 100%;
width: 100%;
}
#roman_numeral {
border-color: black;
border-style: solid;
border-radius: 100;
border-width: 1px;
height: 2em;
}