-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_array1.html
More file actions
119 lines (105 loc) · 2.53 KB
/
js_array1.html
File metadata and controls
119 lines (105 loc) · 2.53 KB
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
<!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>Document</title>
</head>
<body>
<h1>JS Arrays 1</h1>
<div class="row">
<div class="col md-6">
<h3>Array :</h3>
<h3>[<span id="array"></span>]</h3>
</div>
<div class="col md-6" id="actions">
<h3>Actions</h3>
<button type="button" id="sort" class="btn btn-primary">Sort</button>
<button type="button" id="revsort" class="btn btn-primary">Reverse Sort</button>
<button type="button" id="first5" class="btn btn-primary">First 5</button>
<button type="button" id="last5" class="btn btn-primary">Last 5</button>
<button type="button" id="avg" class="btn btn-primary">Average</button>
</div>
</div>
</body>
</html>
<style>
@import url('https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css');
h1 {
text-align: center;
margin: 10px;
}
h3 {
text-align: center;
}
#actions {
margin: auto;
}
.btn{
margin-bottom: 2px;
}
</style>
<script>
window.onload = function () {
init();
};
var arr = [];
for (let i = 0; i < 10; i++) {
arr.push(Math.floor(Math.random() * 100));
}
function init(params) {
var ele = document.getElementById('array');
console.log(arr);
ele.innerText = arr;
}
var arrcopy=[]
for(let j=0;j<arr.length;j++){
arrcopy[j]=arr[j];}
//sort
let sort=document.getElementById("sort");
sort.addEventListener('click',function(){
arrcopy.sort(function(a,b){ return a-b});
alert(arrcopy.toString());
// arrcopy=[]
// console.log("copy",arrcopy)
})
//reversesort
let reverse=document.getElementById("revsort");
reverse.addEventListener('click',function(){
var arrcopy1=[]
for(let j=0;j<arr.length;j++){
arrcopy1[j]=arr[j];}
// console.log("reverse copy",arrcopy1)
arrcopy1.sort(function(a,b){ return a-b});
arrcopy1.reverse();
alert(arrcopy1.toString());
})
//first five
let first5=document.getElementById("first5");
first5.addEventListener('click',function(){
const a=arr.filter(function(e,i){
return i<5;
})
alert(a.toString());
})
//last five
let last5=document.getElementById("last5");
last5.addEventListener('click',function(){
const b=arr.filter(function(e,i){
return i>=5;
})
alert(b.toString());
})
//average
let avg=document.getElementById("avg");
avg.addEventListener('click',function(){
let sum
avg.addEventListener("click",function(){
sum=arr.reduce(function(a,e){
return a+e;
})
alert(sum/10);
})
})
</script>