-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbananas.js
More file actions
23 lines (20 loc) · 705 Bytes
/
Copy pathbananas.js
File metadata and controls
23 lines (20 loc) · 705 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* Bananas kata
https://www.codewars.com/kata/5917fbed9f4056205a00001e
*/
var bananas = function(s, word='banana', current='', length=0) {
length = s.length > length ? s.length : length;
if (s.length ===0 && current.length === length ) {
if (/^.*b.*a.*n.*a.*n.*a.*$/gi.test(current)) return [current]
}
const words = [];
for (let i = 0; i < s.length; i ++) {
const letter = s[0];
if (letter === word[0]) {
const res = bananas(s.slice(i+1), word.slice(1), current + letter, length)
words.push(...res)
} const resDash = bananas(s.slice(i+1), word, current + '-', length)
words.push(...resDash)
}
if (current === '') console.log(words)
return words;
}