title: Bulls & Cows in JavaScript
body: <p>I have been struggling with the LeetCode <a href="https://leetcode.com/problems/bulls-and-cows/" rel="nofollow">'Bulls &amp; Cows'</a> challenge for the last day.  I am a front-end developer in the process of learning Algorithms, and am having a hard time understanding a block of logic achieved in Java and porting it over to my JavaScript solution.</p>
  
  <p>The working <a href="https://leetcode.com/discuss/67031/one-pass-java-solution" rel="nofollow">Java Solution</a>:</p>
  
  <pre><code>public String getHint(String secret, String guess) {
      int bulls = 0;
      int cows = 0;
      int[] numbers = new int[10];
      for (int i = 0; i&lt;secret.length(); i++) {
          int s = Character.getNumericValue(secret.charAt(i));
          int g = Character.getNumericValue(guess.charAt(i));
          if (s == g) bulls++;
          else {
              if (numbers[s] &lt; 0) cows++;
              if (numbers[g] &gt; 0) cows++;
              numbers[s] ++;
              numbers[g] --;
          }
      }
      return bulls + "A" + cows + "B";
  }
  </code></pre>
  
  <p>Specifically I am having a difficult time understanding the following block of functionality:</p>
  
  <pre><code>else {
      if (numbers[s] &lt; 0) cows++;
      if (numbers[g] &gt; 0) cows++;
      numbers[s] ++;
      numbers[g] --;
  }
  </code></pre>
  
  <p>Here is my JS solution I have derived that is passing all (known) test cases except for <code>secret = 1807</code> and <code>guess = 7810</code>:</p>
  
  <pre><code>var getHint = function(secret, guess) {
    var bulls = 0;
    var cows = 0;
    var nums = [];
    var checkNums = function(num) {
      if(nums.length) {
        for (var l = 0; l &lt; nums.length; l++) {
          if(num === nums[l]) {
            return false;
          } else {
            nums.push(num);
            cows++;
  
            return true;
          }
        } else {
          // /nums/ is equal to 0
          cows++;
          nums.push(num);
        }
      }
    };
  
    if(guess) {
      // iterate over the secret to compare it to the guess
      for (var i = 0; i &lt; secret.length; i++) {
        // compare the related location to check for bulls
        if(secret[i] === guess[i]) {
          bulls++;
          nums.push(guess[i]);
        } else {
          // We didnt find a bull, lets check the /guess/ for cows
          for(var j = 0; j &lt; guess.length; j++) {
            // We have a match, what should we do with it?
            if (secret[i] === guess[j]) {
  
                checkNums(guess[j]);
  
            }
          }
        }
      }
    }
  
    return bulls + "A" + cows + "B";
  };
  </code></pre>
  
  <p>The failed test case I get is:</p>
  
  <pre><code>Input:
  "1807"
  "7810"
  
  Output:
  "1A2B"
  
  Expected:
  "1A3B"
  </code></pre>
  
  <p>I would love to better understand how I can replicate the elegant Java solution better, as well as any ways I can make this JS solution more succinct (and working).  The more I bang my head against this, the more <code>for</code> loops and <code>if</code> blocks I wind up writing and I just know I am moving in the wrong direction.</p>
  
  <p>Here is a JSBin of my solution:</p>
  
  <p><a href="http://jsbin.com/jibusa/edit?js,console" rel="nofollow">http://jsbin.com/jibusa/edit?js,console</a></p>
  
  <p>Thank you for any help.</p>