-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockPaperScissorRecurse.js
More file actions
32 lines (30 loc) · 974 Bytes
/
Copy pathRockPaperScissorRecurse.js
File metadata and controls
32 lines (30 loc) · 974 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
// Given the number of rounds in a RPS game, find all the possibilities
// for that number of rounds
// Input: 1, Output: ['r', 'p' ,'s']
// Input: 2, Output: ['rr', 'rp', 'rs', 'pr', 'pp', 'ps', 'sr', 'sp', 'ss']
// Input: 0, Output: []
const rps = (roundCount) => {
// choices in an array
let options = ['r', 'p', 's']
let result = []
// recursive function that takes in a string. build length should be
// the same as the roundCount
const recurse = (build) => {
// Base: if the round is equal to the length of the build, we push to array.
if (roundCount === build.length) {
result.push(build)
return
}
// Loop through the choices and keep adding choices
for (let i = 0; i < options.length; i++) {
// pass in the current build and recurse
recurse(build + options[i])
}
}
// As long as round count is > 0, then recurse with an empty string
if (roundCount > 0) {
recurse('')
}
return result
}
rps(3)