-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
50 lines (42 loc) · 879 Bytes
/
index.js
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
function move(arr1, arr2) {
var counter = 0;
for (var i = 0; i < arr2.length; i++) {
arr1[counter] = arr1[counter] + arr2[i];
counter++;
if (counter >= arr1.length) {
counter = 0;
}
}
return arr1;
}
function uniqueElements(arr) {
return arr.filter(function(v, i) { return i == arr.lastIndexOf(v); });
}
function split(beat) {
var leftSide = [];
var rightSide = [];
for (var i = 0; i < beat.length; i++) {
if (beat[0] === beat[i]) {
leftSide.push(beat[i]);
} else {
rightSide.push(beat[i]);
}
}
return {
leftSide : leftSide,
rightSide : rightSide
}
}
function rhythm(length, beats) {
var beat = Array(length);
beat.fill('-', 0, length);
beat.fill('.', 0, beats);
while(uniqueElements(beat).length === 2) {
beat = move(
split(beat).leftSide,
split(beat).rightSide
);
}
return beat.join('');
}
module.exports = rhythm;