Trello.com Careers Question

by Gerald Gillespie

JavaScript

(function(h, answerLength, limit){

  var
   letterSet = "acdegilmnoprstuw",
   reduction = 7,
   answered = false,
   pos = 0,
   ltrIdx = 0,
   answer = "",
   hash = h,
   ct = 0;

  var reduce = function(){
  	var oldhash; 
  
 //   console.log(answer,":",hash, ltrIdx, pos);
  
  	// answer is the current guess being built up. 
    // answer is also effectively an array representing our past guesses / progress
    // we make a guess 1 letter position at a time. 
    // when a rule is violated we can "backup a step" and modify our guess-- 
    //   incrementing based on the "progress" so far
    // seed is one letter
    // hash is our hash that is being reduced. Our goal is reached when we have both an answer
    // that is the right length and a hash that has been reduced to the hash seed ("reduction")
    seed =  letterSet[ltrIdx];
    
    //cache hash for regression
    oldhash = hash
    //reduce hash
    
    hash = (hash - letterSet.indexOf(seed))/37;
   // console.log("seed:",seed, "answer:", answer, "hash:", hash, oldhash, letterSet.indexOf(seed));
    
    var backup = false;
    if ((hash % 1) === 0) {
    	// hash divides evenly ==> so far so good
    	if( hash > reduction && answer.length < (answerLength - 1) ){
      	// potentially the right track; update our answer and keep going
 	      answer = seed + answer;
        ltrIdx = 0; 
        return false;       
      } else if (hash == reduction && answer.length == ( answerLength-1)){
      	// found the answer? 
        answer = seed + answer;
        return true;
      } else if (hash == reduction){
      	// right hash but wrong answer (answer is too long) so back up a position
      } else if (answer.length == answerLength){
      	// wrong hash but right length so backup up a position
      }
    } else {
    	// hash is not an integer
      if( ltrIdx < letterSet.length ){
        //have more letters to try for this same position
        // (increment the seed)
        ltrIdx++;
       ...