-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ex3.js
52 lines (49 loc) · 1.47 KB
/
Ex3.js
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
let array = [5,25,13,8,45,6,11];
//Find maximum and minimum number
function findMinMax(){
let max = array[0], min = array[0];
for(let i =1;i<array.length;i++){
if(max < array[i]) max = array[i];
if(min > array[i]) min = array[i];
}
return [max, min];
}
//Sorting array(Smallest to largest)
function ascendingSorting(){
for(let i=0;i<array.length;i++){
for(let j=i+1;j<array.length;j++){
if(array[i] > array[j]){
let temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array;
}
//Sorting array(Largest to smallest)
function descendingSorting(){
for(let i=0;i<array.length;i++){
for(let j=i+1;j<array.length;j++){
if(array[i] < array[j]){
let temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array;
}
//Write result using DOM
function result(){
let res = `
<h1>Ex3-Lab1-Web programming</h1>
<h2>Using JavaScript (the given array is [5,25,13,8,45,6,11]):</h2>
<h3>The largest number is ${findMinMax()[0]}</h3>
<h3>The smallest number is ${findMinMax()[1]}</h3>
<h3>Sorting array from smallest to largest: ${ascendingSorting()}</h3>
<h3>Sorting array from largest to smallest: ${descendingSorting()}</h3>
`;
let body = document.getElementById('js');
body.innerHTML = res;
}