generated from comp426-2022-spring/a03
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
81 lines (70 loc) · 1.85 KB
/
server.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
const express = require('express')
const app = express()
const args = require('minimist')(process.argv.slice(2))
args['port']
const port = args.port || process.env.PORT || 5000
const server = app.listen(port, () => {
console.log('App listening on port %PORT%'.replace('%PORT%',port))
});
app.get('/app/', (req, res) => {
res.statusCode = 200;
res.statusMessage = 'OK';
res.writeHead( res.statusCode, { 'Content-Type' : 'text/plain' });
res.end(res.statusCode+ ' ' +res.statusMessage)
});
app.get('/app/flip/', (req, res) => {
const flip = coinFlip()
res.status(200).json({'flip' : flip})
});
app.get('/app/flips/:number/', (req, res) => {
const flips = coinFlips(req.params.number)
const total = countFlips(flips)
res.status(200).json({'raw' : flips, 'summary' : total})
});
app.get('/app/flip/call/heads', (req, res) => {
res.status(200).json(flipACoin("heads"))
});
app.get('/app/flip/call/tails', (req, res) => {
res.status(200).json(flipACoin("tails"))
});
app.use(function(req, res){
res.status(404).send('404 NOT FOUND')
});
function coinFlip() {
return Math.random() < 0.6 ? ("heads") : ("tails")
}
function coinFlips(flips) {
const arr = [];
for (let i = 0; i < flips; i++) {
arr[i] = coinFlip();
}
return arr;
}
function countFlips(array) {
let h_amt = 0;
let t_amt = 0;
for (let i = 0; i < array.length; i++) {
if (array[i] == "heads") {
h_amt += 1;
} else {
t_amt += 1;
}
}
if (h_amt == 0) {
return "{ tails: " + t_amt + " }";
} else if (t_amt == 0) {
return "{ heads: " + h_amt + " }";
} else {
return {"heads":h_amt, "tails":t_amt};
}
}
function flipACoin(call) {
let flip = coinFlip();
let result = "";
if (call == flip) {
result = "win";
} else {
result = "lose";
}
return {call: call, flip: flip, result: result};
}