0% found this document useful (0 votes)
6 views

CSS - Practical 3

Uploaded by

anasqureshi6556
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)
6 views

CSS - Practical 3

Uploaded by

anasqureshi6556
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

CSS- PRACTICAL 3

Name:- Anas Qureshi


Roll No:- 220440
---------------------------------------------------------------------------------------------------------------------------
Q1. Program to create, read and display the array using literal and constructor.
Code: -
<html>
<head>
<title>Anas Qureshi-220440</title>
<script>
let array_literal=[]
let length_array_1=Number.parseInt(prompt("Enter the length of array 1"))
for(let i=0;i<length_array_1;i++){
array_literal[i]=prompt("Enter "+i+" element in array1")
}
document.write("Anas Qureshi-220440 <hr>")
document.write("Array which is made using array literals:-<br>")
document.write(array_literal+"<hr>")
let array_constructor=new Array()
let length_array_2=Number.parseInt(prompt("Enter the length of array 2"))
for(let i=0;i<length_array_2;i++){
array_constructor[i]=prompt("Enter "+i+" element in array 2")
}
document.write("Array which is made using array constructor:-<br>")
document.write(array_constructor)
</script>
</head>
<body>
</body>
</html>
Output:-
Q2. Write a program to sort an array.
Code: -
<html>
<head>
<title>Anas Qureshi-220440</title>
<script>
document.write("Anas Qureshi-220440<hr>")
let array = [10, 5, 40, 30, 50, 60, 70, 15, 35]
const asscending_order = (a, b) => {
return a - b
}
array.sort(asscending_order)
document.write("Array in ascending order<br>"+array+"<hr>")
const descending_order = (a, b) => {
return b - a
}
array.sort(descending_order)
document.write("Array in descending order<br>"+array)
</script>
</head>
<body>
</body>
</html>
Output:-
Q3. Write a program to print the length of array.
Code:-
<html>
<head>
<title>Anas Qureshi-220440</title>
<script>
document.write("Anas Qureshi-220440<hr>")
let array = [10, 5, 40, 30, 50, 60, 70, 15, 35]
document.write(`Lenght of array [${array}] is:-<br>`+array.length)
</script>
</head>
<body>
</body>
</html>
Output:-

Q4. Write a program to display sparse array.


Code:-
<html>
<head>
<title>Anas Qureshi-220440</title>
<script>
document.write("Anas Qureshi-220440<hr>")
let array = [10,,40,,,50,,, 35]
document.write(`Sparse Array:-<br>`+`[${array}]`)
</script>
</head>
<body>
</body>
</html>
Output:-

Q5. Write a program to show the usage of splice method.


Code:-
<html>
<head>
<title>Anas Qureshi-220440</title>
<script>
document.write("Anas Qureshi-220440<hr>")
let array = [10, 5, 40, 30, 50, 60, 70, 15, 35]
document.write(`Array before splice:-<br>[${array}]<hr>`)
array.splice(2,2,"Spliced Element 1","Spliced Element 2","Spliced Element 3")
document.write("Array after splice:-<br>"+`[${array}]`)
</script>
</head>
<body>
</body>
</html>
Output:-

Q6. Write a program to insert element in an array using different method.


Code:-
<html>
<head>
<title>Anas Qureshi-220440</title>
<script>
document.write("Anas Qureshi-220440<hr>")
let array = [10, 5, 40, 30, 50, 60, 70, 15, 35]
document.write(`Array before inserting any element:-<br>[${array}]<hr>`)
document.write(`Inserting element in array using push(), pushing "MHSSP" in array, it will insert
"MHSSP" at the end of array-<br>`)
array.push("MHSSP")
document.write(`[${array}]`+`<hr>`)
document.write(`Now inserting "COMP" in array using splice()-<br>`)
array.splice(3,0,"COMP")
document.write(`[${array}]`+`<hr>`)
document.write(`Now inserting "Saboo" in array using unshift(), which will insert "Saboo" at the
begining of array-<br>`)
array.unshift("Saboo")
document.write(`[${array}]`)
</script>
</head>
<body>
</body>
</html>
Output:-

Q7. Write a program to delete the element from the array using different methods.
Code:-
<html>
<head>
<title>Anas Qureshi-220440</title>
<script>
document.write("Anas Qureshi-220440<hr>")
let array = [10, 5, 40, 30, 50, 60, 70, 15, 35]
document.write(`Array before deleting any element:-<br>[${array}]<hr>`)
document.write(`Deleting element in array using pop(), it will delete the last element of
array<br>`)
array.pop()
document.write(`[${array}]`+`<hr>`)
document.write(`Now deleting element in array using delete-<br>`)
delete array[3]
document.write(`[${array}]`+`<hr>`)
document.write(`Now deleting element in array using shift(), which will delete element from
begining of array-<br>`)
array.shift()
document.write(`[${array}]`)
</script>
</head>
<body>
</body>
</html>
Output:-

You might also like