/
vibe.js
74 lines (61 loc) · 2.29 KB
/
vibe.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
"use strict";
/**
* Copyright 2009 Nicholas C. Zakas. All rights reserved.
* MIT-Licensed
* Uses a binary search algorithm to locate a value in the specified array.
* @param {Array} items The array containing the item.
* @param {variant} value The value to search for.
* @return {int} The zero-based index of the value in the array or -1 if not found.
*/
function binarySearch(items, value){
var startIndex = 0,
stopIndex = items.length - 1,
middle = Math.floor((stopIndex + startIndex)/2);
while(items[middle] != value && startIndex < stopIndex){
//adjust search area
if (value < items[middle]){
stopIndex = middle - 1;
} else if (value > items[middle]){
startIndex = middle + 1;
}
//recalculate middle
middle = Math.floor((stopIndex + startIndex)/2);
}
//make sure it's the right value
return (items[middle] != value) ? -1 : middle;
}
function match() {
var controlVibe = ["adventurous", "ambitious", "compassionate"]
var controlVision = ["entrepreneur", "successful", "travel"]
var vibe = document.getElementById("vibe").value;
var vision = document.getElementById("vision").value;
vibe = vibe.split(',');
vision = vision.split(',');
vibe.sort()
vision.sort()
var numVibeMatch = 0;
var numVisionMatch = 0;
// For each descriptor x in vibe, search for x in controlVibe
for (var i = 0; i < vibe.length; i++) {
if (binarySearch(controlVibe, vibe[i]) != -1) {
numVibeMatch += 1;
}
}
var percentVibeMatch = 1.0*numVibeMatch/3;
for (var i = 0; i < vision.length; i++) {
if (binarySearch(controlVision, vision[i]) != -1) {
numVisionMatch += 1;
}
}
var percentVisionMatch = 1.0*numVisionMatch/3;
var percentages = new Array(percentVibeMatch, percentVisionMatch);
console.log("Percent match in vibe: " + percentVibeMatch);
console.log("Percent match in vision: " + percentVisionMatch);
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://schangwv:mymL2b!@ds157901.mlab.com:57901/vibematch";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
}