Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (65 loc) · 1.73 KB
/
Copy pathindex.js
File metadata and controls
76 lines (65 loc) · 1.73 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
export function analyzeDiceRolls(allRolls){
let resultDiceRolls = 0;
for(let i = 0; i < allRolls.length; i++){
let diceRoll = allRolls[i]
if(diceRoll.length != 5){
throw new Error("Le nombre de dés doit être de 5")
}
for(let j=0; j<diceRoll.length; j++){
if(diceRoll[j]< 1 || diceRoll[j] > 6){
throw new Error("le dé n'est pas compris entre 1 et 6")
}
}
resultDiceRolls += getPoints(diceRoll)
}
return resultDiceRolls;
}
export function getCounts(diceRoll) {
let dieCount = {}
for(let i = 0; i < diceRoll.length; i++){
let value = diceRoll[i]
if(dieCount[value]){
dieCount[value] += 1
}
else{
dieCount[value] = 1
}
}
return dieCount
}
export function getPoints(diceRoll){
let counts = getCounts(diceRoll)
let hasBrelan = false
let hasPair = false
for(let value in counts){
if(counts[value] == 5){
return 50
}
if(counts[value] == 4){
return 35
}
if(counts[value] == 3){
hasBrelan = true
}
if(counts[value] == 2){
hasPair = true
}
}
if(hasBrelan && hasPair){
return 30
}
if(hasBrelan && !hasPair){
return 28
}
let sortedDiceRoll = [...diceRoll].sort((a,b) => a-b)
let seq1 = [1, 2, 3, 4, 5]
let seq2 = [2, 3, 4, 5, 6]
if(sortedDiceRoll.join(',') == seq1.join(',') || sortedDiceRoll.join(',') == seq2.join(',') ){
return 40
}
let sumOfDiceRoll = 0
for(let i = 0; i<diceRoll.length; i++){
sumOfDiceRoll += diceRoll[i]
}
return sumOfDiceRoll
}