0% found this document useful (0 votes)
2 views7 pages

assignament no 2

The document contains six HTML pages, each implementing different functionalities using JavaScript. These include a factorial calculator, palindrome checker, sum of digits of a four-digit number, Fibonacci series generator, array analysis for even, odd, and prime counts, and a text analyzer for counting vowels, consonants, and words. Each page has input fields, buttons for triggering calculations, and displays results dynamically.

Uploaded by

sdking1811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

assignament no 2

The document contains six HTML pages, each implementing different functionalities using JavaScript. These include a factorial calculator, palindrome checker, sum of digits of a four-digit number, Fibonacci series generator, array analysis for even, odd, and prime counts, and a text analyzer for counting vowels, consonants, and words. Each page has input fields, buttons for triggering calculations, and displays results dynamically.

Uploaded by

sdking1811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Question no.

1)
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Factorial Calculator</title>

</head>

<body>

<h2>Factorial Calculator</h2>

<p>Enter a number to find its factorial:</p>

<input type="number" id="numberInput">

<button onclick="calculateFactorial()">Calculate Factorial</button>

<p id="result"></p>

<script>

function factorial(n) {

if (n === 0 || n === 1) {

return 1;

} else {

let result = 1;

for (let i = 2; i <= n; i++) {

result *= i;

return result;

function calculateFactorial() {

const num = document.getElementById('numberInput').value;

const resultElement = document.getElementById('result');

if (num === '') {

resultElement.textContent = 'Please enter a number.';

} else {

const factorialResult = factorial(parseInt(num));

resultElement.textContent = `Factorial of ${num} is: ${factorialResult}`;

</script>

</body>

</html>
Quistion no. 2)
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Palindrome Checker</title>

</head>

<body>

<h2>Palindrome Checker</h2>

<p>Enter a string or number to check if it's a palindrome:</p>

<input type="text" id="input">

<button onclick="checkPalindrome()">Check Palindrome</button>

<p id="result"></p>

<script>

function isPalindrome(input) {

// Convert input to string

const str = String(input);

// Reverse the string

const reversedStr = str.split('').reverse().join('');

// Check if the original string is equal to the reversed string

return str === reversedStr;

function checkPalindrome() {

const input = document.getElementById('input').value;

const resultElement = document.getElementById('result');

if (input === '') {

resultElement.textContent = 'Please enter a string or number.';

} else {

const isPalindromeResult = isPalindrome(input);

if (isPalindromeResult) {

resultElement.textContent = `"${input}" is a palindrome.`;

} else {

resultElement.textContent = `"${input}" is not a palindrome.`;

</script>

</body>

</html>
Question 3)
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Sum of Digits of a Four-Digit Number</title>

</head>

<body>

<h2>Sum of Digits of a Four-Digit Number</h2>

<p>Enter a four-digit number to calculate the sum of its digits:</p>

<input type="number" id="numberInput">

<button onclick="calculateSum()">Calculate Sum</button>

<p id="result"></p>

<script>

function sumOfDigits(number) {

// Convert number to string

const numString = String(number);

// Check if the number is a four-digit number

if (numString.length !== 4) {

return "Please enter a four-digit number.";

// Initialize sum variable

let sum = 0;

// Loop through each digit and add it to the sum

for (let i = 0; i < numString.length; i++) {

sum += parseInt(numString[i]);

return sum;

function calculateSum() {

const number = document.getElementById('numberInput').value;

const resultElement = document.getElementById('result');

if (number === '') {

resultElement.textContent = 'Please enter a number.';

} else {

const sum = sumOfDigits(number);

resultElement.textContent = `Sum of digits of ${number}: ${sum}`;

</script>

</body>

</html>
Question no 4)
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Fibonacci Series</title>

</head>

<body>

<h2>Fibonacci Series</h2>

<p>Enter the value of 'n' to find the nth term of Fibonacci Series:</p>

<input type="number" id="nthTermInput">

<button onclick="printFibonacciSeries()">Print Fibonacci Series</button>

<button onclick="findAndDisplayNthTerm()">Find nth Term</button>

<p id="fibonacciSeriesResult"></p>

<p id="nthTermResult"></p>

<script>

// Function to print Fibonacci series up to 100

function printFibonacciSeries() {

let a = 0, b = 1, nextTerm;

let series = "Fibonacci Series: " + a + ", " + b;

nextTerm = a + b;

while (nextTerm <= 100) {

series += ", " + nextTerm;

a = b;

b = nextTerm;

nextTerm = a + b;

document.getElementById('fibonacciSeriesResult').textContent = series;

// Function to find nth term of Fibonacci series

function findNthTerm(n) {

let a = 0, b = 1, nextTerm;

for (let i = 3; i <= n; i++) {

nextTerm = a + b;

a = b;

b = nextTerm;

return b;

function findAndDisplayNthTerm() {

const nthTermInput = document.getElementById('nthTermInput').value;

const resultElement = document.getElementById('nthTermResult');

if (nthTermInput === '') {


resultElement.textContent = 'Please enter a value for n.';

} else {

const nthTerm = parseInt(nthTermInput);

const term = findNthTerm(nthTerm);

resultElement.textContent = `The ${nthTerm}th term of Fibonacci Series is: ${term}`;

</script>

</body>

</html>

Question no 5)
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Array Analysis</title>

</head>

<body>

<h2>Array Analysis</h2>

<button onclick="processArray()">Process Array</button>

<p id="arrayResult"></p>

<p id="evenResult"></p>

<p id="oddResult"></p>

<p id="primeResult"></p>

<script>

function isPrime(num) {

if (num <= 1) {

return false;

for (let i = 2; i <= Math.sqrt(num); i++) {

if (num % i === 0) {

return false;

return true;

function countEvenOddPrimeNumbers(arr) {

let evenCount = 0;

let oddCount = 0;

let primeCount = 0;

for (let i = 0; i < arr.length; i++) {

if (arr[i] % 2 === 0) {

evenCount++;

} else {
oddCount++;

if (isPrime(arr[i])) {

primeCount++;

return { evenCount, oddCount, primeCount };

function processArray() {

const input = prompt("Enter the elements of the array separated by commas:");

const arr = input.split(",").map(Number);

document.getElementById('arrayResult').textContent = "Array: " + arr;

const counts = countEvenOddPrimeNumbers(arr);

document.getElementById('evenResult').textContent = "Total number of even numbers: " + counts.evenCount;

document.getElementById('oddResult').textContent = "Total number of odd numbers: " + counts.oddCount;

document.getElementById('primeResult').textContent = "Total number of prime numbers: " + counts.primeCount;

</script>

</body>

</html>

Question no 6)
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Text Analyzer</title>

</head>

<body>

<h2>Text Analyzer</h2>

<button onclick="analyzeText()">Analyze Text</button>

<p>Enter the text:</p>

<textarea id="textInput" rows="4" cols="50"></textarea>

<p id="vowelResult"></p>

<p id="consonantResult"></p>

<p id="wordResult"></p>

<script>

function countVowelsAndConsonants(text) {

const vowels = 'aeiouAEIOU';

let vowelCount = 0;

let consonantCount = 0;

// Count vowels and consonants

for (let char of text) {


if (vowels.includes(char)) {

vowelCount++;

} else if (char.match(/[a-zA-Z]/)) {

consonantCount++;

return { vowelCount, consonantCount };

function countWords(text) {

const words = text.split(/\s+/).filter(word => word !== '');

return words.length;

function analyzeText() {

const text = document.getElementById('textInput').value;

const vowelResultElement = document.getElementById('vowelResult');

const consonantResultElement = document.getElementById('consonantResult');

const wordResultElement = document.getElementById('wordResult');

const { vowelCount, consonantCount } = countVowelsAndConsonants(text);

const wordCount = countWords(text);

vowelResultElement.textContent = "Number of vowels: " + vowelCount;

consonantResultElement.textContent = "Number of consonants: " + consonantCount;

wordResultElement.textContent = "Number of words: " + wordCount;

</script>

</body>

</html>

You might also like