-
Notifications
You must be signed in to change notification settings - Fork 2
/
MBTI-algorithm-in-javascript.js
148 lines (113 loc) · 4.08 KB
/
MBTI-algorithm-in-javascript.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//Author: Angela Iredia
//Template of the MBTI-algorithm in Javascript
//For post requests on the server-side, use Node.js server with Express.js as the web framework
//start of the time-complexity
var start = 0;
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
let a, b, c, d;
app.post('/process_data', (req, res) => {
if (req.method === 'POST') {
const data = req.body;
// Extract data from the JSON request
a = data.a; // user's first letter of their MBTI personality type
b = data.b; // user's second letter of their MBTI personality type
c = data.c; // user's third letter of their MBTI personality type
d = data.d; // user's fourth letter of their MBTI personality type
// Check if any required parameters are missing
if (a === undefined || b === undefined || c === undefined || d === undefined) {
return res.status(400).json({ error: 'Missing required parameters' });
}
let q = 12.5;
let r = 12.5;
let s = 12.5;
let t = 12.5;
let mbti_user;
let mbti_server;
mbti_user = [[a,q], [b,r], [c,s], [d, t]];
mbti_server ={
1: [["I",q], ["N",r], ["T",s], ["J",t]],
2: [["I",q], ["N",r], ["T",s], ["P",t]],
3: [["E",q], ["N",r], ["T",s], ["J",t]],
4: [["E",q], ["N",r], ["T",s], ["P",t]],
5: [["I",q], ["N",r], ["F",s], ["J",t]],
6: [["I",q], ["N",r], ["F",s], ["P",t]],
7: [["E",q], ["N",r], ["F",s], ["J",t]],
8: [["E",q], ["N",r], ["F",s], ["P",t]],
9: [["I",q], ["S",r], ["T",s], ["J",t]],
10:[["I",q], ["S",r], ["F",s], ["J",t]],
11:[["E",q], ["S",r], ["T",s], ["J",t]],
12:[["E",q], ["S",r], ["F",s], ["J",t]],
13:[["I",q], ["S",r], ["T",s], ["P",t]],
14:[["I",q], ["S",r], ["F",s], ["P",t]],
15:[["E",q], ["S",r], ["T",s], ["P",t]],
16:[["E",q], ["S",r], ["F",s], ["P",t]]
}
function findMatch(mbti_server, mbti_user){
var matches = [];
for (key in mbti_server){
var lst = mbti_server[key];
if (mbti_user[0][0] == lst[0][0]){
u = mbti_user[0][1] + lst[0][1]
}
else{
u = 0;
}
if (mbti_user[1][0] == lst[1][0]){
x = mbti_user[1][1] + lst[1][1]
}
else{
x = 0;
}
if (mbti_user[2][0] == lst[2][0]){
y = mbti_user[2][1] + lst[2][1]
}
else{
y = 0;
}
if (mbti_user[3][0] == lst[3][0]){
z = mbti_user[3][1] + lst[3][1]
}
else{
z = 0;
}
var match_rate = u + x + y + z ;
if (match_rate >= 50){
var result = {
1: lst[0][0],
2: lst[1][0],
3: lst[2][0],
4: lst[3][0],
5: match_rate
}
matches.push(result);
}
}
if (matches.length > 0){
return res.status(200).json(matches);
}
else {
return res.status(404).json({ message: 'No matches found' });
}
}
return findMatch(mbti_server,mbti_user);
}
else if (req.method === 'GET'){
var result = "It uses also GET request";
return result;
}
else {
res.status(405).json({ error: 'Method Not Allowed' });
}
});
//time complexity
var end = performance.now();
var time_c = end - start;
var time ="Results found in: " + time_c + " s";
print(time);
const port = 3000; // Set the desired port number
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});