-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction-leap-yr-add-del-ele-liner-srch.html
139 lines (116 loc) · 4.07 KB
/
function-leap-yr-add-del-ele-liner-srch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<html>
<head>
<title>JS Array</title>
</head>
<body>
<div>
<marquee direction="left">shashank g - 1nt21is413 - 6th sem C sec - 11 April 2023 - web lab task</marquee>
</div>
<hr />
<p>
1) Write a JavaScript program to find the leap years in a given range of years.
<div>result : <button onclick="leap_year()">leap year finder</button></div>
<hr />
2) Write a JavaScript Program to add elements and to delete elements to the existing array at specific positions.
<div>result : <button onclick="add_remove_element()">array operations</button></div>
<hr />
3) Write a JavaScript program to add elements to an array using the prompt. Implement Linear search on the array.
<div>result: <button onclick="ls()">Linear search</button></div>
</p>
<script>
function leap_year()
{
const startYear = parseInt(prompt("Enter the starting year:"));
const endYear = parseInt(prompt("Enter the ending year:"));
let leapYears = "";
for (let year = startYear; year <= endYear; year++) {
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
if (leapYears !== "") {
leapYears += ", ";
}
leapYears += year;
}
}
if (leapYears === "") {
alert("No leap years found in the given range!");
} else {
alert(`The leap years in the given range are: ${leapYears}`);
}
}
function add_remove_element()
{
const size = parseInt(prompt("Enter the size of the array:"));
const arr = [];
let choice = prompt("Enter 'add' or 'delete' to add or delete elements from the array:");
for (let i = 0; i < size; i++) {
arr.push(prompt(`Enter the element at index ${i}:`));
}
if (choice === "add") {
const index = parseInt(prompt("Enter the index at which to add the element:"));
const element = prompt("Enter the element to add:");
arr.splice(index, 0, element);
} else if (choice === "delete") {
const index = parseInt(prompt("Enter the index of the element to delete:"));
arr.splice(index, 1);
} else {
alert("Invalid choice!");
}
alert(`The array is: ${arr}`);
}
function ls()
{
const arr = []; // initialize an empty array
// function to add an element to the array
function addElement() {
const element = prompt("Enter the element to add:"); // take input from the user for the element to add
arr.push(element); // add the element to the array
}
// function to delete an element from the array
function deleteElement() {
const element = prompt("Enter the element to delete:"); // take input from the user for the element to delete
const index = arr.indexOf(element); // find the index of the element in the array
if (index > -1) {
arr.splice(index, 1); // delete the element from the array
alert(`Element '${element}' deleted from the array!`); // display a message in an alert box
} else {
alert(`Element '${element}' not found in the array!`); // display a message in an alert box
}
}
// function to implement linear search on the array
function linearSearch() {
const element = prompt("Enter the element to search:"); // take input from the user for the element to search
const index = arr.indexOf(element); // find the index of the element in the array
if (index > -1) {
alert(`Element '${element}' found at index ${index} in the array!`); // display a message in an alert box
} else {
alert(`Element '${element}' not found in the array!`); // display a message in an alert box
}
}
// take input from the user for the size of the array
const size = parseInt(prompt("Enter the size of the array:"));
// take input from the user for the elements of the array
for (let i = 0; i < size; i++) {
const element = prompt(`Enter the element at index ${i}:`);
arr.push(element);
}
// take input from the user for the choice of operation
let choice = prompt("Enter 'add', 'delete', or 'search' to add, delete, or search elements in the array:");
// perform the chosen operation
switch (choice) {
case "add":
addElement();
break;
case "delete":
deleteElement();
break;
case "search":
linearSearch();
break;
default:
alert("Invalid choice!");
}
alert(`The array is: ${arr}`); // display the updated array in an alert box
}
</script>
</body>
</html>