-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array_insert.html
55 lines (51 loc) · 1.54 KB
/
Array_insert.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Insert Element in Array</title>
</head>
<body>
<h1>Insert Element in Array</h1>
<input id="position" type="text" placeholder="Enter Position" />
<input id="newEl" type="text" placeholder="Enter New Element " />
<button onclick="insertElement()">Insert</button>
<script>
/* let data=[60,30,10,67,40]
let newEl=70;
let position=2;
for(let i=data.length-1; i>=0; i--) {
console.log(data[i]);
if(i>= position){
data[i+1]=data[i];
if(i===position){
data[i]=newEl;
}
}
}
console.log(data)*/
const insertElement = () => {
let data = [60, 30, 10, 67, 40];
let position = document.getElementById("position").value;
position=parseInt(position)
let newEl = document.getElementById("newEl").value;
newEl=parseInt(newEl);
for (let i = data.length - 1; i >= 0; i--) {
// console.log(data[i]);
if (i >= position) {
data[i + 1] = data[i];
if (i === position) {
data[i] = newEl;
}
}
}
console.log(data);
};
// ********default method***********
let abhi=[87,57,98,68];
abhi.splice(2,0,41)
console.log(abhi)
</script>
</body>
</html>