Skip to content

Commit 3e32ae9

Browse files
committed
Minor format changes to JS problem 271
1 parent 4b01fce commit 3e32ae9

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

javascript/271-Encode-and-Decode-Strings.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22
* @param {string[]} strs
33
* @return {string}
44
*/
5-
var encode = function (strs) {
6-
let res = "";
7-
for (const s of strs) res += String(s.length) + "#" + s;
8-
return res;
5+
function encode(strs) {
6+
return strs.map(str => `${str.length}#${str}`).join('');
97
}
108

119
/**
1210
* @param {string} str
1311
* @return {string[]}
1412
*/
15-
var decode = function (str) {
13+
function decode(str) {
1614
const res = [];
1715
let i = 0;
1816

1917
while (i < str.length) {
2018
let j = i;
21-
while (str[j] != "#") j += 1;
19+
while (str[j] !== "#") {
20+
++j;
21+
}
2222

23-
length = Number(str.slice(i, j));
24-
res.push(str.slice(j + 1, j + 1 + length));
25-
i = j + 1 + length;
23+
const len = Number(str.slice(i, j));
24+
res.push(str.slice(++j, j + len));
25+
i = j + len;
2626
}
2727

2828
return res;

0 commit comments

Comments
 (0)