Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Formatting Software License Key in JavaScript
Problem
We need to write a JavaScript function that formats a software license key by reorganizing alphanumeric characters into groups of a specified length, separated by dashes. The function takes a string containing alphanumeric characters and dashes, removes existing dashes, converts all letters to uppercase, and regroups the characters.
The requirements are:
Remove all existing dashes from the input string
Convert all lowercase letters to uppercase
Group characters into sections of length K (except the first group, which can be shorter but must contain at least one character)
Separate groups with dashes
Example Input and Output
For example, if the input is:
const str = '8-4B0t37-k'; const num = 4;
The output should be:
const output = '84B0-T37K';
Output Explanation
The string has been processed as follows:
Original: '8-4B0t37-k'
Remove dashes: '84B0t37k'
Convert to uppercase: '84B0T37K'
Group into sections of 4: '84B0' and 'T37K'
Result: '84B0-T37K'
Solution
const str = '8-4B0t37-k';
const num = 4;
const formatKey = (str = '', num = 1) => {
let acc = '';
let flag = num;
for(let i = str.length - 1; i >= 0; i--) {
const char = str.charAt(i);
if(char !== '-') {
if(flag === 0) {
acc = `-${acc}`;
flag = num;
}
acc = `${char.toUpperCase()}${acc}`;
flag -= 1;
}
}
return acc;
};
console.log(formatKey(str, num));
84B0-T37K
How It Works
The algorithm processes the string from right to left (reverse iteration) to ensure proper grouping:
Reverse Iteration: We iterate from the end to accommodate cases where the first group has fewer than K characters
Character Processing: Skip dashes and convert valid characters to uppercase
Group Formation: Use a counter (flag) to track characters in the current group
Dash Insertion: When a group reaches the specified length, insert a dash and reset the counter
Alternative Approach
Here's a more straightforward approach using string methods:
const formatLicenseKey = (str, k) => {
// Remove dashes and convert to uppercase
const cleaned = str.replace(/-/g, '').toUpperCase();
// Calculate first group length
const firstGroupLength = cleaned.length % k;
let result = '';
// Add first group if it exists
if (firstGroupLength > 0) {
result = cleaned.slice(0, firstGroupLength);
}
// Add remaining groups
for (let i = firstGroupLength; i < cleaned.length; i += k) {
if (result.length > 0) result += '-';
result += cleaned.slice(i, i + k);
}
return result;
};
// Test with the same example
console.log(formatLicenseKey('8-4B0t37-k', 4));
console.log(formatLicenseKey('2-4A0r7-4k', 3));
84B0-T37K 24-A0R-74K
Key Points
The algorithm ensures the first group can be shorter than K characters
All subsequent groups have exactly K characters
Reverse iteration simplifies the grouping logic
The alternative approach is more readable but uses additional string operations
Conclusion
Both approaches effectively format license keys by removing dashes, converting to uppercase, and regrouping characters. The reverse iteration method is more efficient, while the string-based approach is more intuitive to understand.
