-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_basic.js
More file actions
106 lines (94 loc) · 2.41 KB
/
Copy patharray_basic.js
File metadata and controls
106 lines (94 loc) · 2.41 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
// 1-Write a function to find the largest element in an array.
let k = [2,5,7,8,4] ;
function theLargestInArray(k){
let long=k.length;
let x= k[0];
for (let i=1;i<long;i++){
if(x< k[i]){
x=k[i];
}
}
return x;
}
console.log(theLargestInArray(k));
// 2-Write a function to find the smallest element in an array.
// function theSmallestInArray(k){
// let long=k.length;
// let y= k[0];
// for (let i=1;i<long;i++){
// if(y> k[i]){
// y=k[i];
// }
// }
// return y;
// }
// 3-Write a function to find the sum of all elements in an array.
function sum(k){
let long=k.length;
let y= k[0];
for (let i=1;i<long;i++){
y+=k[i];
}
console.log(y);
return y;
}
sum(k);
// 4-Write a function to find the average of all elements in an array.
function avg(k){
long=k.length;
return sum(k)/long;
}
console.log(avg(k))
// 5-Write a function to find the median of all elements in an array.
// function median(sort(k)){
// if(long%2==0){
// let z= (newk[long/2]+newk[long/2-1])/2
// }else{
// let z=newk[(long-1)/2]
// }
// }
function median(mid){
newArr=mid.sort(function(a, b){return a - b});
long=mid.length;
if(long%2==0){
let z= (newArr[long/2]+newArr[long/2-1])/2
}else{
let z=newArr[(long-1)/2]
}
return z;
}
// 6-Write a function to remove all duplicates from an array.
let c=0
function counter(c){
return ++c;
}
function removeAllDuplicates(){
let newArray;
let v=newk[0];
newArray[0]=v;
for (i=1;i<long;i++){
if (v !==newk[i]){
v=newk[i]
newArray[counter(c)]=v
}
}
return newArray;
}
// 7-Write a function to sort an array in ascending order.
function myFunction2() {
points.sort(function(a, b){return a - b});
document.getElementById("demo").innerHTML = points;
}
// 8-Write a function to sort an array in descending order.you can use reverse from 7
points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo1").innerHTML = points;
points.sort(function(a, b){return b - a});
document.getElementById("demo2").innerHTML = points;
// 9-Write a function to shuffle the elements of an array randomly.
const points = [40, 100, 1, 5, 25, 10];
for (let i = points.length -1; i > 0; i--) {
let j = Math.floor(Math.random() * (i+1));
let k = points[i];
points[i] = points[j];
points[j] = k;
}