-
Notifications
You must be signed in to change notification settings - Fork 0
/
level3.js
85 lines (75 loc) · 1.77 KB
/
level3.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
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
//GET
//POST
//PUT
//DELETE
//using Postman you can send any request
const express=require("express");
const app=express();
const user=[{
name: "John",
kidney:[{
healthy:false
}]
}];
app.use(express.json()); //mandatory for post req
app.get("/",function(req,res){
const johnKidneys=user[0].kidney;
const numberOfKidneys=johnKidneys.length;
let noOfHealthyKidneys=0;
for(let i=0;i<numberOfKidneys;i++){
if(johnKidneys[i].healthy){
noOfHealthyKidneys++;
}
}
const noOfUnhealthyKidneys=numberOfKidneys-noOfHealthyKidneys
res.json({
numberOfKidneys,noOfUnhealthyKidneys,noOfHealthyKidneys
})
});
//input healthy and unhealthy kidney
app.post("/",function(req,res){
const isHealthy=req.body.isHealthy;
user[0].kidney.push({
healthy:isHealthy
})
res.json({
msg:"Done!"
})
});
//making all the unhealthy kidney to healthy kidney
app.put("/",function(req,res){
for(let i=0;i<user[0].kidney.length;i++){
user[0].kidney[i].healthy=true;
}
res.json({});
})
//removing all the unhealthy kidneys
app.delete("/",function(req,res){
if(isThereAtLeastOneUnhealthyKidney()){
const newKidney=[]
for(let i=0;i<user[0].kidney.length;i++){
if(user[0].kidney[i].healthy){
newKidney.push({
healthy:true
})
}
}
user[0].kidney=newKidney;
res.json({})
}
else{
res.status(411).json({
msg:"You don't have unhealthy kidney"
})
}
})
function isThereAtLeastOneUnhealthyKidney(){
let atLeastOneUnhealthyKidney=false
for(let i=0;i<user[0].kidney.length;i++){
if(!user[0].kidney[i].healthy){
atLeastOneUnhealthyKidney=true
}
}
return atLeastOneUnhealthyKidney
}
app.listen(3000);