Recently, I created a Factor Calculator tool in HTML and CSS and uploaded it to my webspace. It is running properly, but the calculation isn't complete. You can check it here: https://factorcalculators.com.
Please visit the provided link and enter 24 in the box. When you click enter, it displays results in Factors, Factor Pairs, and Prime Factors. The Factors and Factor Pairs results are correct, but the Prime Factorization isn't fully calculated. It shows only 24 = 2 * 3, which is incorrect. The correct Prime Factorization for the number 24 is 24 = 2 * 2 * 2 * 3. Below, I provide the code for better assistance.
Please visit the provided link and enter 24 in the box. When you click enter, it displays results in Factors, Factor Pairs, and Prime Factors. The Factors and Factor Pairs results are correct, but the Prime Factorization isn't fully calculated. It shows only 24 = 2 * 3, which is incorrect. The correct Prime Factorization for the number 24 is 24 = 2 * 2 * 2 * 3. Below, I provide the code for better assistance.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Advance Factor Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.calculator-container {
width: 100%; /* Set width to 100% for full width */
padding: 20px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
}
input[type="number"] {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
width: 100%;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
padding: 10px 0;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
#result {
margin-top: 20px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
#result h3 {
margin-bottom: 5px;
}
#result p {
margin: 0;
}
.step {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="calculator-container">
<h1>Advance Factor Calculator</h1>
<p>Use our Advanced version of Factor Calculator where you can find <b>Factors, Factor Pairs and Prime Factor</b> of any number. Enter a number below and press “Enter” or click “Calculate” to find its factors, factor pairs, and prime factors.</p>
<p><strong>Example:</strong> If you enter 12, the calculator will show that the factors are 1, 2, 3, 4, 6, and 12.</p>
<label for="numberInput">Enter a number:</label>
<input type="number" id="numberInput">
<button onclick="calculateFactors()">Calculate</button>
<div id="result"></div>
</div>
<script>
// Add event listener to input field to detect Enter key press
document.getElementById("numberInput").addEventListener("keydown", function(event) {
if (event.keyCode === 13) {
calculateFactors();
}
});
function calculateFactors() {
var number = parseInt(document.getElementById("numberInput").value);
var factors = [];
var factorPairs = [];
var primeFactors = [];
for (var i = 1; i <= number; i++) {
if (number % i === 0) {
factors.push(i);
factorPairs.push([i, number / i]);
}
}
for (var j = 0; j < factors.length; j++) {
if (isPrime(factors[j])) {
primeFactors.push(factors[j]);
}
}
displayResults(factors, factorPairs, primeFactors, number);
}
function isPrime(num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 === 0 || num % 3 === 0) return false;
for (var i = 5; i * i <= num; i += 6) {
if (num % i === 0 || num % (i + 2) === 0) return false;
}
return true;
}
function displayResults(factors, factorPairs, primeFactors, number) {
var resultDiv = document.getElementById("result");
var primeFactorsString = primeFactors.join(" × ");
resultDiv.innerHTML = "<h3>Results:</h3>" +
"<div class='step'><strong>Factors:</strong> " + factors.join(", ") + "</div>" +
"<div class='step'><strong>Factor Pairs:</strong><br>" + formatFactorPairs(factorPairs) + "</div>" +
"<div class='step'><strong>Prime Factors:</strong> " + number + " = " + primeFactorsString + "</div>";
}
function formatFactorPairs(pairs) {
var formattedPairs = "";
pairs.forEach(pair => {
formattedPairs += "(" + pair[0] + ", " + pair[1] + ")<br>";
});
return formattedPairs;
}
</script>
</body>
</html>