We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3d12f6e commit 71c12dfCopy full SHA for 71c12df
299-bulls-and-cows.js
@@ -0,0 +1,29 @@
1
+/**
2
+ * @param {string} secret
3
+ * @param {string} guess
4
+ * @return {string}
5
+ */
6
+const getHint = function(secret, guess) {
7
+ let bulls = 0
8
+ let cows = 0
9
+ const h = {}
10
+ for(let i = 0, len = secret.length; i < len; i++) {
11
+ if(secret[i] === guess[i]) {
12
+ bulls++
13
+ } else {
14
+ if(!h.hasOwnProperty(secret[i])) h[secret[i]] = 0
15
+ h[secret[i]]++
16
+ }
17
18
+
19
20
+ if(secret[i] !== guess[i]) {
21
+ if(h.hasOwnProperty(guess[i]) && h[guess[i]] > 0) {
22
+ cows++
23
+ h[guess[i]]--
24
25
26
27
28
+ return `${bulls}A${cows}B`
29
+};
0 commit comments