License Key Formatting LeetCode solution in JavaScript

1
// Please upvote if you like the solution. Thanks
2

3
var licenseKeyFormatting = function (s, k) {
4
let str = s.replace(/[^A-Za-z0-9]/g, "").toUpperCase();
5
let ans = "";
6
let i = str.length;
7
while (i > 0) {
8
ans = "-" + str.substring(i - k, i) + ans; // we are taking k characters from the end of string and adding it to answer
9
i = i - k;
10
}
11
return ans.substring(1); // removing the "-" which is present in the start of ans
12
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0