-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBD2.4-CW.js
53 lines (41 loc) · 1.42 KB
/
BD2.4-CW.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
53
const express = require('express');
const app = express();
let ages = [25, 30, 18, 22, 27];
let students = [{"name" : "Rahul", "rollNo": 101, "marks": 85},
{"name" : "Sita", "rollNo": 102, "marks": 95},
{"name" : "Amit", "rollNo": 103, "marks": 70}];
let cars = [
{ make: "Maruti", model: "Swift", mileage: 22 },
{ make: "Hyundai", model: "i20", mileage: 18 },
{ make: "Tata", model: "Nexon", mileage: 21 }
];
function sortAgesAscending(ages) {
return ages.sort((a, b) => a - b);
}
app.get('/ages/sort-ascending', (req, res) => {
let sortedAges = sortAgesAscending(ages);
res.json(sortedAges);
});
function sortAgesDescending (ages) {
return ages.sort((a, b) => b - a);
}
app.get('/ages/sort-descending', (req, res) => {
let sortedAges = sortAgesDescending (ages);
res.json(sortedAges);
});
function sortStudentsByMarksDescending(students) {
return students.sort((a, b) => b.marks - a.marks);
}
app.get('/students/sort-by-marks-descending', (req, res) => {
let sortedStudents = sortStudentsByMarksDescending(students);
res.json(sortedStudents);
});
function sortCarsByMileageDescending(cars) {
return cars.sort((a, b) => b.mileage - a.mileage);
}
app.get('/cars/sort-by-mileage-descending', (req, res) => {
let sortedCars = sortCarsByMileageDescending(cars);
res.json(sortedCars);
});
const PORT = 3009;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));