-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaster.js
More file actions
37 lines (28 loc) · 777 Bytes
/
master.js
File metadata and controls
37 lines (28 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const evaluate = (secret, guess) => {
let correctColors = 0,
correctPositions = 0
correctPositions = findCorrectPosition(secret, guess)
correctColors = findCorrectColors(secret, guess)
return [correctPositions, correctColors]
}
const findCorrectPosition = (secret, guess) => {
let correctPositions = 0
correctPositions = secret.reduce((p, current, index) => {
return p + (current == guess[index])
}, 0)
return correctPositions
}
const findCorrectColors = (secret, guess) => {
let correctColors = 0
for (let i = 0; i < secret.length; i++) {
for (let j = 0; j < secret.length; j++) {
if (secret[i] == guess[j] && i != j) {
correctColors++
}
}
}
return correctColors
}
module.exports = {
evaluate
}