1

I am creating a chat script, where a user can set their interests. When a user connects to the server their client sends the following JSON over WebSocket:

{"id": int, "hash": md5, "automessage": {...}, "interests": ["cars", "programming", "stackoverflow"]}

When the first connection is received it is pushed to a waiting array. Every time another connection starts it removes the last object from the array to 'pair' them together. What I now need to do is write a function so that when it receives the message it it looks at the interests value of all the objects in the waiting array and returns the object with the most common interests. For example, if the waiting array looks like this:

[
    {"id": int, "hash": md5, "automessage": {...}, "interests": ["cats", "animals", "cars"]},
    {"id": int, "hash": md5, "automessage": {...}, "interests": ["programming", "ssh", "stackoverflow"]},
    {"id": int, "hash": md5, "automessage": {...}, "interests": ["climbing", "football", "coffee"]}
]

Now when the above message is received it looks through the above array, and returns the object one with the most similar interests. So, in this example it would return {"id": int, "hash": md5, "automessage": {...}, "interests": ["programming", "ssh", "stackoverflow"]}.

If it cannot find any entries with similar interests, then the user should be added to the waiting list array.

I'm pretty stuck with this, so can anyone help?


Not really sure why the question got downvoted. Care to add a comment?

Benedict Lewis
  • 2,733
  • 7
  • 37
  • 78

2 Answers2

2

I'll answer the "find the closest element" part:

function intersection(a, b) {
    return a.filter(function(x) { return b.indexOf(x) >= 0 })
}

function closest(ary, arrays) {
    return arrays.map(function(x) {
        return [intersection(ary, x).length, x]
    }).sort(function(a, b) {
        return b[0] - a[0]
    })[0][1]
}

Example:

me = ["cars", "programming", "stackoverflow"]

interests = [
    ["cats", "animals", "cars"],
    ["programming", "ssh", "stackoverflow"],
    ["climbing", "football", "coffee"]
]

console.log(closest(me, interests))
> programming,ssh,stackoverflow
georg
  • 211,518
  • 52
  • 313
  • 390
1

DEMO

You can try this to find from the waiting list the best candidate, then if not found you can add it to

 var incoming = {
    "id": 'int',
        "hash": 'md5',
        "automessage": {},
        "interests": ["cars", "programming", "stackoverflow"],
};

var waiting_list = [{
    "id": 'int',
        "hash": 'md5',
        "automessage": {},
        "interests": ["cats", "animals", "cars"]
}, {
    "id": 'int',
        "hash": 'md5',
        "automessage": {},
        "interests": ["programming", "ssh", "stackoverflow"]
}, {
    "id": 'int',
        "hash": 'md5',
        "automessage": {},
        "interests": ["climbing", "football", "coffee"]
}];

// var exists = (myNumbers.indexOf(bar) > -1); //true

var largerCount = 0, index; // will contain the count & index of largest match
for (var i = 0; i < waiting_list.length; i++) { // iterate over the waiting list
    var current = waiting_list[i];
    var currentCount = 0; // get the current match count
    var incoming_array = incoming.interests; // get the incoming interest
    for (var j = 0; j < incoming_array.length; j++) {

        if(current.interests.indexOf(incoming_array[j]) > -1) { 
           currentCount++; // add to count if match is found
        }
        if(currentCount > largerCount) { // if current count is bigger then assign it to largerCounr
            largerCount = currentCount;
            index = i; // assign the index of match
        }
    }
    currentCount = 0;
}

if(index >= 0) {
console.log(waiting_list[index]); // print the match
} else {
    // add to waiting list
    waiting_list.push(incoming);
}
Raunak Kathuria
  • 3,185
  • 1
  • 18
  • 27
  • Nice, but there is a problem with `if(index)` because if `index = 0` (which is could be) then `if` treats it as false. Quick demo at http://jsfiddle.net/BenedictLewis/j3VWk/ – Benedict Lewis Jan 10 '14 at 13:07
  • Thanks for the updated. You can change it to index >= 0 it should work. Will update the answer as well – Raunak Kathuria Jan 10 '14 at 13:27
  • Thanks. I found that `index != null` also works as well (http://stackoverflow.com/a/5113396/2078412) – Benedict Lewis Jan 10 '14 at 13:37
  • Another bug I've just found, which is that if the array is empty then it doesn't match any users. – Benedict Lewis Jan 11 '14 at 11:27
  • If you want to check for empty array and then push it to the waiting list you can check for length and push it. It was base answer you can modify as per your need – Raunak Kathuria Jan 12 '14 at 04:35