-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (46 loc) · 1.41 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
import express from 'express'
import startups from './startups-mock'
const app = express()
const port = 3000
// with recursive
const interestMatch = (interests, startups, index = startups.length - 1, matches = []) => {
if (index < 0) {
return matches
} else {
for (let i = 0; i < interests.length; i++) {
if (startups[index].interests.indexOf(interests[i]) !== -1) {
const duplicate = matches.filter(match => (match.name === startups[index].name))
if (duplicate.length < 1) {
matches.push(startups[index])
}
}
}
return interestMatch(interests, startups, index - 1, matches)
}
}
// without recursive
const interestMatchNonRecursive = (interests, startups) => {
let matches = []
for (let i = 0; i < interests.length; i++) {
for (let x = 0; x < startups.length; x++) {
if (startups[x].interests.indexOf(interests[i]) !== -1) {
const duplicate = matches.filter(match => (match.name === startups[x].name))
if (duplicate.length < 1) {
matches.push(startups[x])
}
}
}
}
return matches
}
// Why do we use recursive?
app.use(express.json())
app.get('/', (req, res) => {
res.send(startups)
})
app.post('/match', (req, res) => {
const { interests } = req.body
const matches = interestMatch(interests, startups)
res.send(matches)
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))