Practical File[1]
Practical File[1]
Output:
<html>
<head>
<title>swap method 2.</title>
</head>
<body>
<script>
var a=5;
var b=10;
document.write("before swap: a=", a,",b=",b);
[a,b] = [b,a];
document.write("After swap: a=", a,",b=",b);
</script>
</body></html>
Output: same
Q2. Write a code snipped in javascript which input 2
numbers and display the square of largest number
among them. (using ternary operator)
Code:
<html>
<head><title>Q2</title>
</head>
<body>
<script>
var num1= parseInt(prompt("enter first number"));
var num2= parseInt(prompt("enter second number"));
let squareOfLargest = (num1 > num2 ? num1 : num2)**2;
document.write("the square of the largest number is:",
squareOfLargest);
</script>
</body>
</html>
Input:
Enter first number: 6
Enter second number 7
Output:
Q3. Write a code snipped in javascript which input an
alphabet and check whether it is a vowel or not.
Code:
<html>
<head><title>Q3</title>
</head>
<body>
<script>
var alphabet=parseInt(prompt("enter a vowel"));
switch(alphabet) {
case"a":
case"e":
case"i":
case"o":
case"u":
document.write(alphabet+ "is a vowel.");
break;
default:
document.write(alphabet+"is not a vowel.");
}
</script>
</body>
</html>
Output:
</script>
</body>
</html>
Input:
Choose an option: 1(square)
Enter the side of the square:25
Output:
Code:
<html>
<head><title>Q6</title>
</head>
<body>
<script>
var basicSalary = parseInt(prompt("Enter the basic salary
of the employee:"));
var hraPercentage = basicSalary <= 15000 ? 0.05 : 0.10;
var daPercentage = basicSalary <= 15000 ? 0.07 : 0.15;
var hra = basicSalary * hraPercentage;
var da = basicSalary * daPercentage;
var grossSalary = basicSalary + hra + da;
document.write("Basic Salary: " + basicSalary + "<br>");
document.write("HRA: " + hra + "<br>");
document.write("DA: " + da + "<br>");
document.write("Gross Salary: " + grossSalary);
</script>
</body>
</html>
Input:
Enter the basic salary: 13000
Output:
Output:
Q9.write a code snippet in javascript to check whether
number is prime or not.
Code:
<html>
<head><title>Q9</title>
</head>
<body>
<script>
var n = parseInt(prompt("Enter a number to check if it is
prime:"));
var i;
for (i = 2; i <= n - 1; i++) {
if (n % i === 0) {
document.write("It is not a prime number");
break;
} else {
document.write("It is a prime number");
}
}
</script>
</body>
</html>
Input:
Enter the number: 8
Output:
Output:
Q11. Write a code snippet in javascript to display an array
of size 6. After displaying the array remove the last
element.
Code:
<html>
<head>
<title>Q11</title>
</head>
<body>
<script>
const number=[10, 20, 30, 40, 50, 60];
document.write("Original array:", number +"<br>");
number.pop();
document.write("Array after removing the last
element:",number);
</script>
</body>
</html>
Output:
</script>
</body>
</html>
Output:
function getUserInputs() {
const principal = parseInt(prompt("Enter the principal
amount:"));
const rate = parseInt(prompt("Enter the rate of interest
(in %):"));
const time = parseInt(prompt("Enter the time
period:"));
const interestType = prompt("Enter the type of interest
(simple/compound):").toLowerCase();
Input:
Enter weight in kilograms: 8
Output:
Q21.write a program with function sortnum() to sort 3
numbers in asending order.
Code:
<html>
<head><title>Q21</title>
</head>
<script>
function sortnum(a,b,c){
if(a>b){
[a,b]=[b,a];
}
if(a>c){
[a,c]=[c,a];
}
if(b>c){
[b,c]=[c,b];
}
return[a,b,c];
}
let num1= prompt("enter first number");
let num2= prompt("enter second number");
let num3= prompt("enter third number");
let sortedNums =
sortnum(parseInt(num1),parseInt(num2),parseInt(num3)
);
document.write("sorted numbers:" + sortedNums);
</script>
</html>
Input:
Enter first number=9
Enter second number=1
Enter third number=5
Output:
<button onclick="pytha()">Check</button>
<p id="result"></p>
<script>
function pytha() {
let a =
parseInt(document.getElementById("side1").value);
let b =
parseInt(document.getElementById("side2").value);
let c =
parseInt(document.getElementById("side3").value);
<script>
function stringjava() {
let str1 = document.getElementById("str1").value;
let str2 = document.getElementById("str2").value;
// Convert to lowercase
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
let result = "";
// Search for str1 in str2
if (str2.includes(str1)) {
result += str1 + " found in String 2<br>";
} else {
result += str1 + " not found in String 2<br>";
}
// Replace "i" with "!" in str2
str2 = str2.replace(/i/g, "!");
result += "Modified String 2: " + str2 + "<br>";
// Display first character of str1
result += "First character of String 1: " + str1[0];
document.getElementById("result").innerHTML = result;
}
</script>
</body>
</html>
Input:
Enter string1: “hello”
Enter string2: "this is hello world"
Output:
Code:
<html>
<head><title>Q26.</title>
</head>
<body>
<script>
const cars=["Honda","BMW","Audi","porsche"];
document.write("Original array:<br>",cars);
{
cars.push("volvo");
document.write("<br>a)Adding volvo in the
array:<br>",cars);
}
{
cars.shift();
document.write("<br>b)After removing first element
from the array:<br>",cars);
}
</script>
</body>
</html>
Output:
document.getElementById("result").innerHTML =
result;
}
</script>
</body>
</html>
Input:
Enter string: life is beautiful
Output:
Q28.write a function :
• To find the value of a number raised power another
number using Math object method
• To find the sqrt of a numbering using Math object
method.
Code:
<html>
<head><title>Q28.JavaScript Math Functions</title>
</head>
<body>
<h2>JavaScript Math Functions</h2>
<input id="base" type="number"
placeholder="Base">
<input id="exponent" type="number"
placeholder="Exponent">
<button onclick="power()">Calculate
Power</button>
<div id="powerResult"></div>
<input id="number" type="number"
placeholder="Number">
<button onclick="sqrt()">Calculate Square
Root</button>
<div id="sqrtResult"></div>
<script>
// Function to find the value of a number raised to
the power of another number
function power() {
let base = document.getElementById("base").value;
let exponent =
document.getElementById("exponent").value;
let result = Math.pow(base, exponent);
document.getElementById("powerResult").innerHT
ML = base + " raised to the power of " + exponent + "
is: " + result;
}
// Function to find the square root of a number
function sqrt() {
let number =
document.getElementById("number").value;
let result = Math.sqrt(number);
document.getElementById("sqrtResult").innerHTML
= "Square root of " + number + " is: " + result;
}
</script>
</body>
</html>
Input:
Base:2
Exponent:3
Number:16
Output:
Q29. Write a program with a function called through a
button to ask user age if the age is greater than 18,
display the message “you are eligible”.
Code:
html>
<head>
<title>Q29.Age Eligibility Checker</title>
</head>
<body>
<h2>Age Eligibility Checker</h2>
<input id="age" type="number" placeholder="Enter your
age">
<button onclick="checkEligibility()">Check
Eligibility</button>
<div id="result"></div>
<script>
function checkEligibility() {
let age = document.getElementById("age").value;
if (age >= 18) {
document.getElementById("result").innerHTML = "You
are eligible!";
} else if (age < 18 && age >= 0) {
document.getElementById("result").innerHTML = "You
are not eligible. You must be 18 or older.";
} else {
document.getElementById("result").innerHTML =
"Invalid age. Please enter a valid number.";
}
}
</script>
</body>
</html>
Input:
Enter your age:18;
Output:
Output:
Output: