You are playing the Bulls and Cows game with your friend.
    You write down a secret number and ask your friend to guess what the number is. When your friend makes a guess, you provide a hint with the following info:

  • The number of “bulls”, which are digits in the guess that are in the correct position.
  • The number of “cows”, which are digits in the guess that are in your secret number but are located in the wrong position.
  • Specifically, the non-bull digits in the guess that could be rearranged such that they become bulls.
    Given the secret number secret and your friend’s guess guess, return the hint for your friend’s guess.
    The hint should be formatted as “xAyB”, where x is the number of bulls and y is the number of cows. Note that both secret and guess may contain duplicate digits.

    你和朋友正在玩一個遊戲。
    你寫下一個隱藏號碼,讓你的朋友猜猜這個號碼是什麼。當你的朋友進行猜測時,你提供以下的提示:
    - bulls 的數量,即猜中的數字,且位置正確。
    - cows 的數量,即猜中的數字,但位置錯誤。
    
    具體來說,未符合 bulls 的數字可以重新排列,直到符合 bulls。
    給予特定隱藏數字和朋友的猜測,並回傳提示。
    提示的格式為 'xAyB',其中 x 是 bulls 的數量,y 是 cows 的數量。
    請注意密碼和猜測都有可能包含重複的數字。
    

    Example 1:

    Input: secret = "1807", guess = "7810"
    Output: "1A3B"
    Explanation: Bulls are connected with a '|' and cows are underlined:
    "1807"
      |
    "7810"
    

    Example 2:

    Input: secret = "1123", guess = "0111"
    Output: "1A1B"
    Explanation: Bulls are connected with a '|' and cows are underlined:
    "1123"        "1123"
      |      or     |
    "0111"        "0111"
    Note that only one of the two unmatched 1s is counted as a cow since the non-bull digits can only be rearranged to allow one 1 to be a bull.
    

    Solution:
    1. 宣告 bull 和 cow 分別為 0,sBox 和 gBox 為 []。
    (ex. 個別計算正確的數量、正確+位置錯誤的數量,以及存放待確認數字的空間。
    2. 將猜的數字進行一輪數字與位置的判定,皆符合,則 bull++,只要其中一個不符合,則存入空間。
    3. 運用 indexOf 找出符合數字的位置,位置符合,則 cow++,當沒有符合的時候 indexOf會回傳 -1,則不列入計算。
    4. 回傳 xAyB。

    Code:

    var getHint = function(secret, guess) {
      let bull = 0, cow = 0
      let sBox = [], gBox = []
    
      for (i = 0; i < guess.length; i++) {
        if (secret[i] === guess[i]) {
          bull++
        } else {
          sBox.push(secret[i])
          gBox.push(guess[i])
        }
      }
    
      for (j = 0; j < gBox.length; j++) {
        let findIndex = sBox.indexOf(gBox[j])
    
        if (findIndex !== -1) {
          cow++
          sBox[findIndex] = null
        }
      }
      return bull + "A" + cow + "B"
    };
    

    FlowChart:
    Example 1

    Input: secret = "1807", guess = "7810"
    
    step.1
                             secret = "1807", guess = "7810"
    secret[1] === guess[1] =>           V               V
    bull = 0 + 1 = 1
    sBox = ["1", "0", "7"]
    gBox = ["7", "1", "0"]
    
    step.2
    sBox = ["1", "0", "7"], gBox = ["7", "1", "0"]
             V                            V    
    cow = 0 + 1 = 1
    
    sBox = ["0", "7"], gBox = ["7", "0"]
             O                       O
    cow = 1 + 1 = 2
    
    sBox = ["7"], gBox = ["7"]
             #             #
    cow = 2 + 1 = 3
    
    return bull + "A" + cow + "B" => 1A3B
    

    Example 2

    Input: secret = "1123", guess = "0111"
    
    step.1
                             secret = "1123", guess = "0111"
    secret[1] === guess[1] =>           V               V
    bull = 0 + 1 = 1
    sBox = ["1", "2", "3"]
    gBox = ["0", "1", "1"]
    
    step.2
    sBox = ["1", "2", "3"], gBox = ["0", "1", "1"]
             V                            V    
    cow = 0 + 1 = 1
    
    sBox = ["2", "3"], gBox = ["0", "1"]
             X    X             X    X
    
    return bull + "A" + cow + "B" => 1A1B
    

    .push()

    .push():在陣列中的末端添加一個或多個元素,且增加該陣列的長度。
    
    const animals = ['pigs', 'goats', 'sheep'];
    animals.length =   1   ,    2   ,    3
    
    const count = animals.push('cows');
    console.log(count);
    // expected output: 4
    console.log(animals);
    // expected output: Array ["pigs", "goats", "sheep", "cows"]
    
    animals.push('chickens', 'cats', 'dogs');
    console.log(animals);
    // expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]
    

    .indexOf()

    .indexOf():回傳指定元素在陣列中第一個找到的位置,若不存在,則回傳 -1。
    
    const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
    Array.beasts =    0   ,    1   ,   2   ,    3  ,    4
    
    console.log(beasts.indexOf('bison'));
    // expected output: 1
    
    // start from index 2
    console.log(beasts.indexOf('bison', 2));
    // expected output: 4
    
    console.log(beasts.indexOf('giraffe'));
    // expected output: -1