-
Notifications
You must be signed in to change notification settings - Fork 0
/
kata.js
28 lines (20 loc) · 1004 Bytes
/
kata.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
function separateLiquids(glass) {
let pos = ['O', 'A', 'W', 'H'];
if (glass.length === 0) return [];
let sorted = glass.toString().split(',').sort((a, b) => pos.indexOf(a) - pos.indexOf(b));
let arr = [];
for (let i = 0; i < sorted.length; i += glass[0].length) {
arr.push(sorted.slice(i, i + glass[0].length))
}
return arr;
}
console.log(separateLiquids([['H', 'H', 'W', 'O'], ['W', 'W', 'O', 'W'], ['H', 'H', 'O', 'O']]));
// [['O', 'O', 'O', 'O'],['W','W','W','W'],['H','H','H','H']], "");
console.log(separateLiquids([['A', 'A', 'O', 'H'], ['A', 'H', 'W', 'O'], ['W', 'W', 'A', 'W'], ['H', 'H', 'O', 'O']]));
// [['O','O','O','O'],['A', 'A', 'A', 'A'],['W','W','W','W'],['H','H','H','H']], "");
console.log(separateLiquids([['A', 'H', 'W', 'O']]));
//[['O','A','W','H']],"");
console.log(separateLiquids([['A'], ['H'], ['W'], ['O']]));
//[['O'],['A'],['W'],['H']],"")
console.log(separateLiquids([]));
// [], "Empty array should be returned if given.");