0

So my girlfriend and her sisters were playing around with this iPhone app where you could enter two names and it would return a % compatibility match. I started playing with it too (no shame) and was trying to figure out how it worked. If you compared two exact strings, e.g., "John Doe" and "John Doe" it would return a somewhat random compatibility, but it would return the same compatibility every time. So it wasn't really "random".

Does anyone have any idea on how I could get a function to do the same in JavaScript? Here's my shell function:

 function getCompatibility(name1, name2) {
   // Lower case name1 and name2 and remove spaces
   name1 = name1.replace(' ', '').toLowerCase();
   name2 = name2.replace(' ', '').toLowerCase();

   // Do some type of comparison to get a value between 0 and 100 back
   // HELP ME HERE =]
 }

 getCompatibility('John Doe', 'John Doe'); // outputs 60% (or something)
 getCompatibility('John Doe', 'John Doe'); // outputs 60% (same as above)
 getCompatibility('John Resig', 'Angelina Jolie'); // outputs 90% (or something)
Kirk Ouimet
  • 27,280
  • 43
  • 127
  • 177

2 Answers2

2

You can calculate a number based on the strings (say, add the ASCII values of each character), and seed it to a random number generator. That way, it outputs the same when the strings don't change.

edit: for doing this in javascript, check out this question.

Community
  • 1
  • 1
cambraca
  • 27,014
  • 16
  • 68
  • 99
  • Can you explain the process of seeding it to a random number generator? – Kirk Ouimet Nov 26 '10 at 00:34
  • Well pseudo-random number generators work in some deterministic way, and often there is a "seed" function (in php it is [srand](http://php.net/manual/en/function.srand.php)), so that if you seed a certain number to it, then the sequence of the generated "random" numbers from then on will always be the same. JS doesn't have that, so the solution would be to actually implement one of the pseudo random number generators. – cambraca Nov 26 '10 at 00:38
  • I don't know much about the actual details of pseudo-random number generators, but there are ready-made functions one can copy&paste. – cambraca Nov 26 '10 at 00:39
0

Doesn't really sound very interesting. I'd prefer something like Toby Segaran's "Programming Collective Intelligence" Chapter 9: 'Advanced Classification: Kernel Methods and SVM'. It uses a matchmaker database to show you how to do it on different characteristics. I think this would be much more interesting.

What you're recommending could be little more than looking up a percentage in an associative array that's generated by a random number generator.

duffymo
  • 305,152
  • 44
  • 369
  • 561