0% found this document useful (0 votes)
34 views31 pages

Q1

Uploaded by

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

Q1

Uploaded by

rahhuuullllll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q1

<!DOCTYPE html>

<html>

<head>

<title>Swap Two Numbers</title>

</head>

<body>

<h2>Swap Two Numbers</h2>

<label>Enter first number: </label>

<input type="number" id="num1"><br><br>

<label>Enter second number: </label>

<input type="number" id="num2"><br><br>

<button onclick="swapNumbers()">Swap</button><br><br>

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

<script>

function swapNumbers() {

let a = document.getElementById("num1").value;

let b = document.getElementById("num2").value;

// Convert to numbers

a = Number(a);

b = Number(b);

// Swap using temp variable

let temp = a;

a = b;

b = temp;

document.getElementById("result").innerHTML =

"After swapping:<br>First Number: " + a + "<br>Second Number: " + b;

</script>

</body>

</html>
Q2

<!DOCTYPE html>

<html>

<head>

<title>Square of Larger Number</title>

</head>

<body>

<h2>Square of the Larger Number</h2>

<label>Enter first number: </label>

<input type="number" id="num1"><br><br>

<label>Enter second number: </label>

<input type="number" id="num2"><br><br>

<button onclick="findSquare()">Find Square</button>

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

<script>

function findSquare() {

// Get input values

let a = parseFloat(document.getElementById("num1").value);

let b = parseFloat(document.getElementById("num2").value);

// Find the larger number

let larger = (a > b) ? a : b;

// Calculate square

let square = larger * larger;

// Show result

document.getElementById("result").innerHTML =

"The larger number is " + larger + " and its square is " + square;

</script>

</body>

</html>
Q3
// Take input from the user
let alphabet = prompt("Enter an alphabet:");
// Convert the input to lowercase
alphabet = alphabet.toLowerCase();
// Check if the input is a single alphabet letter
if (alphabet.length === 1 && alphabet.match(/[a-z]/)) {
// Check if it's a vowel
if ("aeiou".includes(alphabet)) {
alert(alphabet + " is a vowel.");
} else {
alert(alphabet + " is not a vowel.");
}
} else {
alert("Please enter a single valid alphabet letter.");
}
Q4
<!DOCTYPE html>
<html>
<head>
<title>Area Calculator</title>
</head>
<body>
<h2>Area Calculator</h2>
<label for="choice">Choose an option:</label>
<select id="choice">
<option value="1">Area of Square</option>
<option value="2">Area of Triangle</option>
<option value="3">Surface Area of Cuboid</option>
</select>
<br><br>
<button onclick="calculateArea()">Calculate</button>
<p id="result"></p>
<script>
function calculateArea() {
let choice = document.getElementById("choice").value;
let result = "";
switch (parseInt(choice)) {
case 1:
let side = parseFloat(prompt("Enter the side of the square:"));
result = "Area of Square = " + (side * side);
break;
case 2:
let base = parseFloat(prompt("Enter the base of the triangle:"));
let height = parseFloat(prompt("Enter the height of the triangle:"));
result = "Area of Triangle = " + (0.5 * base * height);
break;

case 3:
let length = parseFloat(prompt("Enter the length of the cuboid:"));
let width = parseFloat(prompt("Enter the width of the cuboid:"));
let heightCuboid = parseFloat(prompt("Enter the height of the cuboid:"));
let surfaceArea = 2 * (length * width + width * heightCuboid +
heightCuboid * length);
result = "Surface Area of Cuboid = " + surfaceArea;
break;

default:
result = "Invalid choice!";
}

document.getElementById("result").innerText = result;
}
</script>
</body>
</html>
Q5

<!DOCTYPE html>

<html>

<head>

<title>Net Amount Calculator</title>

</head>

<body>

<h2>Net Amount Calculator</h2>

<label>Enter Quantity:</label>

<input type="number" id="quantity"><br><br>

<label>Enter Price per Item:</label>

<input type="number" id="price"><br><br>

<button onclick="calculateNetAmount()">Calculate</button>

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

<script>

function calculateNetAmount() {

let quantity = parseFloat(document.getElementById("quantity").value);

let price = parseFloat(document.getElementById("price").value);

let total = quantity * price;

let discount = 0.15 * total;

let netAmount = total - discount;

document.getElementById("result").innerHTML =

"Total Amount: " + total.toFixed(2) + "<br>" +

"Discount (15%): " + discount.toFixed(2) + "<br>" +

"Net Amount: " + netAmount.toFixed(2);

</script>

</body>

</html>
Q6

<!DOCTYPE html>

<html>

<head>

<title>Gross Salary Calculator</title>

</head>

<body>

<h2>Gross Salary Calculator</h2>

<label>Enter Basic Salary:</label>

<input type="number" id="basic"><br><br>

<button onclick="calculateGross()">Calculate</button>

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

<script>

function calculateGross() {

let basic = parseFloat(document.getElementById("basic").value);

let hra, da;

if (basic <= 15000) {

hra = 0.05 * basic;

da = 0.07 * basic;

} else {

hra = 0.10 * basic;

da = 0.15 * basic;

let gross = basic + hra + da;

document.getElementById("result").innerHTML =

"Basic Salary: " + basic.toFixed(2) + "<br>" +

"HRA: " + hra.toFixed(2) + "<br>" +

"DA: " + da.toFixed(2) + "<br>" +

"<b>Gross Salary: " + gross.toFixed(2) + "</b>";

</script>
Q7 <!DOCTYPE html>

<html>

<head>

<title>Fibonacci Series</title>

</head>

<body>

<h2>Fibonacci Series up to a Number</h2>

<label>Enter a number:</label>

<input type="number" id="num"><br><br>

<button onclick="generateFibonacci()">Show Fibonacci Series</button>

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

<script>

// User-defined function to generate Fibonacci series till a given number

function fibonacci(n) {

let a = 0, b = 1;

let series = [];

while (a <= n) {

series.push(a);

let next = a + b;

a = b;

b = next;

return series;

// Function to call user-defined function and display result

function generateFibonacci() {

let num = parseInt(document.getElementById("num").value);

let result = fibonacci(num);

document.getElementById("result").innerHTML = "Fibonacci Series: " + result.join(", ");

</script>
Q8
<!DOCTYPE html>
<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>

<h2>Factorial Calculator</h2>

<label>Enter a number:</label>
<input type="number" id="num"><br><br>

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

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

<script>
// User-defined function to calculate factorial
function factorial(n) {
let fact = 1;
for (let i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
// Function to read input and call factorial function
function showFactorial() {
let number = parseInt(document.getElementById("num").value);

if (number < 0) {
document.getElementById("result").innerText = "Factorial is not defined
for negative numbers.";
} else {
let result = factorial(number);
document.getElementById("result").innerText = "Factorial of " + number +
" is " + result;
}
}
</script>

</body>
</html>
Q9
<!DOCTYPE html>
<html>
<head>
<title>Prime Number Checker</title>
</head>
<body>

<h2>Check Prime Number</h2>

<label>Enter a number:</label>
<input type="number" id="num"><br><br>

<button onclick="checkPrime()">Check</button>

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

<script>
// User-defined function to check if a number is prime
function isPrime(n) {
if (n <= 1) return false; // 0 and 1 are not prime
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) return false;
}
return true;
}
// Function to get input and display result
function checkPrime() {
let number = parseInt(document.getElementById("num").value);
let resultText = isPrime(number) ? number + " is a Prime Number." : number
+ " is NOT a Prime Number.";
document.getElementById("result").innerText = resultText;
}
</script>

</body>
</html>
Q10

<!DOCTYPE html>

<html>

<head>

<title>Palindrome Checker</title>

</head>

<body>

<h2>Check Palindrome Number</h2>

<label>Enter a number:</label>

<input type="number" id="num"><br><br>

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

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

<script>

// User-defined function to check if a number is palindrome

function isPalindrome(n) {

let original = n.toString();

let reversed = original.split('').reverse().join('');

return original === reversed;

// Function to handle input and display result

function checkPalindrome() {

let number = document.getElementById("num").value;

let resultText = isPalindrome(number)

? number + " is a Palindrome."

: number + " is NOT a Palindrome.";

document.getElementById("result").innerText = resultText;

</script>

</body>

</html>
Q12

<!DOCTYPE html>

<html>

<head>

<title>Kilogram to Gram Converter</title>

</head>

<body>

<h2>Kilogram to Gram Converter (1 kg = 100 grams)</h2>

<label>Enter weight in kilograms:</label>

<input type="number" id="kgInput" placeholder="e.g., 2">

<button onclick="convertToGrams()">Convert</button>

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

<script>

function convertToGrams() {

let kg = parseFloat(document.getElementById("kgInput").value);

if (isNaN(kg)) {

document.getElementById("result").innerText = "Please enter a valid number.";

return;

// Conversion using your rule: 1 kg = 100 grams

let grams = kg * 100;

document.getElementById("result").innerText = kg + " kilograms = " + grams + " grams.";

</script>

</body>

</html>
Q11
<!DOCTYPE html>
<html>
<head>
<title>Interest Calculator</title>
</head>
<body>
<h2>Simple or Compound Interest Calculator</h2>
<label>Enter Principal Amount:</label>
<input type="number" id="principal"><br><br>
<label>Enter Rate of Interest (%):</label>
<input type="number" id="rate"><br><br>
<label>Enter Time (years):</label>
<input type="number" id="time"><br><br>
<label>Choose Interest Type:</label>
<select id="interestType">
<option value="simple">Simple Interest</option>
<option value="compound">Compound Interest</option>
</select><br><br>
<button onclick="calculateInterest()">Calculate</button>
<p id="result"></p>
<script>
// Function to calculate simple interest
function simpleInterest(P, R, T) {
return (P * R * T) / 100;
}
// Function to calculate compound interest
function compoundInterest(P, R, T) {
let amount = P * Math.pow((1 + R / 100), T);
return amount - P;
}
// Function to get inputs and display result
function calculateInterest() {
let P = parseFloat(document.getElementById("principal").value);
let R = parseFloat(document.getElementById("rate").value);
let T = parseFloat(document.getElementById("time").value);
let type = document.getElementById("interestType").value;
let result;
if (type === "simple") {
result = simpleInterest(P, R, T);
document.getElementById("result").innerText = "Simple Interest: " + result.toFixed(2);
} else if (type === "compound") {
result = compoundInterest(P, R, T);
document.getElementById("result").innerText = "Compound Interest: " +
result.toFixed(2);
} else {
document.getElementById("result").innerText = "Please select a valid interest type.";
}
}
</script>
</body>
</html>
Q13<!DOCTYPE html>

<html>

<head>

<title>Smallest of Three Numbers</title>

</head>

<body>

<h2>Find the Smallest of Three Numbers</h2>

<label>Enter first number:</label>

<input type="number" id="num1"><br><br>

<label>Enter second number:</label>

<input type="number" id="num2"><br><br>

<label>Enter third number:</label>

<input type="number" id="num3"><br><br>

<button onclick="findSmallest()">Find Smallest</button>

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

<script>

function findSmallest() {

// Get the numbers from input fields

let a = parseFloat(document.getElementById("num1").value);

let b = parseFloat(document.getElementById("num2").value);

let c = parseFloat(document.getElementById("num3").value);

// Check for valid inputs

if (isNaN(a) || isNaN(b) || isNaN(c)) {

document.getElementById("result").innerText = "Please enter all three numbers.";

return;

// Find the smallest number

let smallest = Math.min(a, b, c);

// Display the result

document.getElementById("result").innerText = "The smallest number is: " + smallest;

</script>

</body>
Q14
<!DOCTYPE html>
<html>
<head>
<title>Welcome Message</title>
</head>
<body>
<h2>Enter Your Name</h2>
<input type="text" id="username" placeholder="Enter your name">
<button onclick="MyFunc()">Submit</button>
<p id="message"></p>
<script>
function MyFunc() {
let name = document.getElementById("username").value;

// Display the welcome message


document.getElementById("message").innerText = "Hello " + name + ",
welcome to this page";
}
</script>
</body>
</html>
Q15 <!DOCTYPE html>
<html>
<head>
<title>String JavaScript Function</title>
</head>
<body>
<h2>String Operation Function (stringjava)</h2>

<label>Enter first string:</label>


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

<label>Enter second string:</label>


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

<button onclick="stringjava()">Run Function</button>

<p id="output"></p>

<script>
function stringjava() {
// Get input values
let string1 = document.getElementById("str1").value;
let string2 = document.getElementById("str2").value;

// Convert both to lowercase


let str1Lower = string1.toLowerCase();
let str2Lower = string2.toLowerCase();

let outputText = "";


// a) Display lowercase strings
outputText += "Lowercase String 1: " + str1Lower + "<br>";
outputText += "Lowercase String 2: " + str2Lower + "<br>";

// b) Search for string1 in string2


if (str2Lower.includes(str1Lower)) {
outputText += "String 1 found in String 2: " + string1 + "<br>";
} else {
outputText += "String 1 not found in String 2.<br>";
}

// c) Replace all 'I' with '!' in string2


let replacedStr2 = string2.replace(/I/g, "!");
outputText += "String 2 after replacing 'I' with '!': " + replacedStr2 + "<br>";

// d) Display first character of string1


if (string1.length > 0) {
outputText += "First character of String 1: " + string1.charAt(0) + "<br>";
} else {
outputText += "String 1 is empty.<br>";
}

// Show result
document.getElementById("output").innerHTML = outputText;
}
</script>
</body>
</html>
Q16

<!DOCTYPE html>

<html>

<head>

<title>String Operation</title>

</head>

<body>

<h2>String Function: mystring()</h2>

<button onclick="mystring()">Run Function</button>

<p id="output"></p>

<script>

function mystring() {

// Define the string

let sentence = "Life is Beautiful";

// a) Display the length of the string

let lengthOfString = sentence.length;

// b) Extract the word "is" using indexOf() and substring()

let index = sentence.indexOf("is");

let word = sentence.substring(index, index + 2); // "is" is 2 characters long

// Display results

let result = "Original String: " + sentence + "<br>";

result += "Length of the string: " + lengthOfString + "<br>";

result += "Extracted word using indexOf and substring: " + word;

document.getElementById("output").innerHTML = result;

</script>

</body>

</html>
Q17 <!DOCTYPE html>

<html>

<head>

<title>Sort Array</title>

</head>

<body>

<h2>Sort Array in Ascending Order</h2>

<button onclick="sortArray()">Sort Array</button>

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

<script>

// Function that accepts an array and sorts it in ascending order

function sortIntegers(arr) {

// Use sort method with compare function

return arr.sort(function(a, b) {

return a - b;

});

function sortArray() {

// Example array of integers

let numbers = [12, 5, 9, 1, 50, 23];

// Call the function to sort

let sorted = sortIntegers(numbers);

// Display the result

document.getElementById("result").innerText = "Sorted Array: " + sorted.join(", ");

</script>

</body>

</html>
Q18
<!DOCTYPE html>
<html>
<head>
<title>Array Manipulations</title>
</head>
<body>
<h2>Array: Cars</h2>
<script>
let cars = ["Honda", "BMW", "Audi", "Porsche"];
cars.push("Volvo");
cars.shift();
let person = ["Ranjan", "Yagya", "Munish"];
cars = cars.concat(person);
let filtered = cars.filter(car => car === "BMW" || car === "Audi");
cars = cars.filter(car => car !== "Audi");
let carStr = cars.join(" - ");
document.write("Updated Cars Array: " + cars + "<br>");
document.write("Filtered Array (BMW, Audi): " + filtered + "<br>");
document.write("String with - : " + carStr);
</script>
</body>
</html>
Q19
<!DOCTYPE html>
<html>
<head>
<title>Fruit Array</title>
</head>
<body>
<h2>Fruit Array Operations</h2>
<script>
let fruits = ["Apple", "Banana", "Cherry", "Date"];
document.write("String: " + fruits.toString() + "<br>");
fruits.pop();
fruits.unshift("Mango");
fruits.reverse();
document.write("Updated Array: " + fruits);
</script>
</body>
</html>
Q20
<!DOCTYPE html>
<html>
<head>
<title>Math Object</title>
</head>
<body>
<h2>Math Object Example</h2>
<script>
let rand = Math.floor(Math.random() * 41) + 10;
document.write("Random number (10-50): " + rand + "<br>");
document.write("Max: " + Math.max(45, 22, 89, 17) + "<br>");
document.write("Min: " + Math.min(45, 22, 89, 17));
</script>
</body>
</html>
Q21
<!DOCTYPE html>
<html>
<head>
<title>Power and Square Root</title>
</head>
<body>
<h2>Math.pow and Math.sqrt</h2>
<script>
let base = parseInt(prompt("Enter base:"));
let exp = parseInt(prompt("Enter exponent:"));
let num = parseInt(prompt("Enter number for square root:"));
document.write("Power: " + Math.pow(base, exp) + "<br>");
document.write("Square Root: " + Math.sqrt(num));
</script>
</body>
</html>
Q22
<!DOCTYPE html>
<html>
<head>
<title>Pythagorean Triplet</title>
</head>
<body>
<h2>Pythagorean Triplet Checker</h2>
<script>
let a = parseInt(prompt("Enter side a:"));
let b = parseInt(prompt("Enter side b:"));
let c = parseInt(prompt("Enter side c:"));
if (a * a + b * b === c * c) {
document.write("It is a Pythagorean Triplet.");
} else {
document.write("It is NOT a Pythagorean Triplet.");
}
</script>
</body>
</html>
Q23
<!DOCTYPE html>
<html>
<head>
<title>Age Eligibility</title>
</head>
<body>
<h2>Check Age Eligibility</h2>
<script>
let age = parseInt(prompt("Enter your age:"));
if (age >= 18) {
document.write("You are eligible.");
} else {
document.write("You are not eligible.");
}
</script>
</body>
</html>
Q24

<!DOCTYPE html>
<html>
<head>
<title>Welcome Alert</title>
</head>
<body onload="welcome()">
<h2>Welcome to Website</h2>
<script>
function welcome() {
alert("Welcome to the website");
}
</script>
</body>
</html>
Q25
<!DOCTYPE html>
<html>
<body onkeydown="eventTriggered()">
<script>
function eventTriggered() {
confirm("Event triggered. Press OK.");
}
</script>
</body>
</html>

You might also like