Skip to content

Commit 2bcf6d6

Browse files
authored
Create 38-count-and-say.js
1 parent 5e80bab commit 2bcf6d6

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

38-count-and-say.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number} n
3+
* @return {string}
4+
*/
5+
const countAndSay = function(n) {
6+
let str = '1'
7+
for (let i = 2; i <= n; i++) {
8+
let tempStr = ''
9+
let count = 0
10+
for (let j = 0, m = str.length; j < m; j++) {
11+
const char = str.charAt(j)
12+
count += 1
13+
if (char !== str.charAt(j + 1)) {
14+
tempStr += count + char
15+
count = 0
16+
}
17+
}
18+
str = tempStr
19+
}
20+
return str
21+
}

0 commit comments

Comments
 (0)