Skip to content

Commit 38da47f

Browse files
committed
countAndSay
1 parent e898ab3 commit 38da47f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

countAndSay.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number} n
3+
* @return {string}
4+
*/
5+
var countAndSay = function(n) {
6+
let index = 0
7+
if (n === 1) return "1"
8+
9+
return iterate(["1"])
10+
11+
function iterate(arr){
12+
let total = 0,
13+
results = "",
14+
lastChar;
15+
16+
while (arr.length){
17+
let char = arr.shift();
18+
if (!lastChar || char === lastChar){
19+
total++
20+
} else {
21+
results += total.toString() + lastChar
22+
total = 1
23+
}
24+
lastChar = char
25+
}
26+
27+
results += total.toString() + lastChar
28+
29+
if (++index < (n - 1)){
30+
return iterate(results.split(""))
31+
}
32+
33+
return results
34+
}
35+
};

0 commit comments

Comments
 (0)