Number of Lines To Write String LeetCode solution in JavaScript

1
var numberOfLines = function (widths, s) {
2
let pixel = 100,
3
line = 1;
4
for (let i = 0; i < s.length; i++) {
5
if (pixel >= widths[s[i].charCodeAt() - 97]) {
6
pixel -= widths[s[i].charCodeAt() - 97];
7
} else {
8
// this word should be written in NEXT line, so it CANNOT count.
9
i--;
10
line++;
11
pixel = 100;
12
}
13
}
14
// 100-pixel = space used in this line.
15
return [line, 100 - pixel];
16
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0