Here’s a JavaScript code that combines several functionalities, including reversing a number, finding the factorial, generating the Fibonacci series, checking for Armstrong numbers, and checking for palindromes. This will provide an interactive menu where the user can choose which operation to perform.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Number Operations</title>
<script>
// Function to reverse a number
function reverseNumber(num) {
return num.toString().split('').reverse().join('');
}
// Function to calculate factorial of a number
function factorial(num) {
if (num === 0 || num === 1) return 1;
let result = 1;
for (let i = 2; i <= num; i++) {
result *= i;
}
return result;
}
// Function to generate Fibonacci series up to n terms
function fibonacci(n) {
let fibSeries = [0, 1];
for (let i = 2; i < n; i++) {
fibSeries.push(fibSeries[i - 1] + fibSeries[i - 2]);
}
return fibSeries;
}
// Function to check if a number is an Armstrong number
function isArmstrong(num) {
let sum = 0;
let temp = num;
const length = num.toString().length;
while (temp > 0) {
let digit = temp % 10;
sum += Math.pow(digit, length);
temp = Math.floor(temp / 10);
}
return sum === num;
}
// Function to check if a number is a palindrome
function isPalindrome(num) {
const str = num.toString();
return str === str.split('').reverse().join('');
}
// Display results based on the user's choice
function performOperation() {
const choice = parseInt(document.getElementById("operation").value);
const inputNumber = parseInt(document.getElementById("inputNumber").value);
let result = '';
switch (choice) {
case 1:
result = `Reversed Number: ${reverseNumber(inputNumber)}`;
break;
case 2:
result = `Factorial: ${factorial(inputNumber)}`;
break;
case 3:
const fibSeries = fibonacci(inputNumber);
result = `Fibonacci Series (first ${inputNumber} terms): ${fibSeries.join(', ')}`;
break;
case 4:
result = isArmstrong(inputNumber) ? `${inputNumber} is an Armstrong number` : `${inputNumber} is NOT an Armstrong number`;
break;
case 5:
result = isPalindrome(inputNumber) ? `${inputNumber} is a Palindrome` : `${inputNumber} is NOT a Palindrome`;
break;
default:
result = "Invalid choice! Please select a valid operation.";
break;
}
document.getElementById("result").innerText = result;
}
</script>
</head>
<body>
<h2>Number Operations</h2>
<label for="inputNumber">Enter a number:</label>
<input type="number" id="inputNumber" placeholder="Enter number" required><br><br>
<label for="operation">Select an operation:</label>
<select id="operation">
<option value="1">Reverse Number</option>
<option value="2">Factorial</option>
<option value="3">Fibonacci Series</option>
<option value="4">Armstrong Number</option>
<option value="5">Palindrome Check</option>
</select><br><br>
<button onclick="performOperation()">Perform Operation</button>
<h3>Result:</h3>
<p id="result"></p>
</body>
</html>
Explanation
Reverse Number: The function reverseNumber takes the input number, converts it to a string, splits it into an array, reverses it, and joins it back together to produce the reversed number.
Factorial: The factorial function calculates the factorial of a number by multiplying all integers from 1 to the given number.
Fibonacci Series: The fibonacci function generates the Fibonacci series up to n terms, where n is the number entered by the user.
Armstrong Number: The isArmstrong function checks if the entered number is an Armstrong number. An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits.
Palindrome: The isPalindrome function checks if the entered number reads the same backward as forward.
How it works
The user enters a number and selects an operation from the dropdown.
When the user clicks the "Perform Operation" button, the corresponding function is called, and the result is displayed below the button.