License Key Formatting LeetCode solution in JavaScript
1// Please upvote if you like the solution. Thanks23var licenseKeyFormatting = function (s, k) {4let str = s.replace(/[^A-Za-z0-9]/g, "").toUpperCase();5let ans = "";6let i = str.length;7while (i > 0) {8ans = "-" + str.substring(i - k, i) + ans; // we are taking k characters from the end of string and adding it to answer9i = i - k;10}11return ans.substring(1); // removing the "-" which is present in the start of ans12};