-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete-array-element.html
71 lines (64 loc) · 2.09 KB
/
delete-array-element.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Array element Deletion</title>
<style>
h1 {
background-color: gold;
padding: 10px;
border-radius: 4px;
display: inline-block;
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<h1>delete an element in array</h1>
<br />
<label for="index">Delete element by index: </label>
<input type="text" id="index" />
<button type="button" onclick="deleteAtIndexDynamically()">
deleteElement
</button>
<!-- script tag here -->
<script>
// Create an array containing the elements.
let array = [44, 11, 23, 26, 33, 10];
function deleteAtIndex(array, index) {
// using loop to delete array element
for (let i = index; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
array.length = array.length - 1;
}
// Example usage:
deleteAtIndex(array, 3);
console.log(array);
// define a faction dynamically deleting the elements in an array.
function deleteAtIndexDynamically() {
let array = [44, 55, 66, 77, 88, 99];
let index = Number(document.getElementById("index").value);
// Check if the index value is a valid number and not greater than the array length
if (isNaN(index) || index > array.length) {
alert(
"Please enter a valid index value that is less than or equal to the array length and ."
);
}
// Check if the element index value is a valid number and not negative
if (isNaN(index) || index < 0) {
alert(
"Please enter a valid element index value that is not negative."
);
}
// using loop delete element from array.
for (let i = index; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
array.length = array.length - 1;
console.log(array);
}
</script>
</body>
</html>