Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GustavoEmmel/02ad52704ede505c4cd935ab2ae6f230 to your computer and use it in GitHub Desktop.
Save GustavoEmmel/02ad52704ede505c4cd935ab2ae6f230 to your computer and use it in GitHub Desktop.
// Input: S = “2-5g-3-J”, K = 2
// Output: “2-5G-3J”
// Input: S = “5F3Z-2e-9-w”, K = 4
// Output: “5F3Z-2E9W”
function formater (chars, k) {
console.log("chars", chars);
console.log("k", k);
chars = chars.replaceAll("-", "")
chars = chars.toUpperCase();
let output = "";
const splited = chars.split("");
const totalLen = splited.length;
console.log("totalLen", totalLen);
const pairs = totalLen / k;
console.log("pairs", pairs);
console.log("pairs", Number.isInteger(pairs));
console.log("ceil", Math.ceil(pairs))
console.log("floor", Math.floor(pairs))
const isEven = Number.isInteger(pairs);
let firstPart = false;
let breakPoint = 0;
const double = (Math.floor(pairs) - 1) * 2;
console.log("double", double);
const multipleLoops = (double > totalLen - 1);
console.log("multipleLoops", multipleLoops)
for(i = 0; i < totalLen; i++) {
output += splited[i];
console.log("pairs", pairs)
if(pairs > 2) {
if(isEven && !firstPart) {
breakPoint = Math.floor(pairs) - 1;
} else {
breakPoint = Math.floor(pairs);
}
} else {
breakPoint = k - 1;
}
console.log("breakPoint");
console.log(splited[i]);
console.log("i", i);
if(i == breakPoint) {
output += "-";
firstPart = true;
} else {
const division = i / breakPoint;
if(multipleLoops && Number.isInteger(division) && i < totalLen -1) {
console.log("i", i);
console.log("totalLen", totalLen)
console.log("splited", splited[i])
output += "-";
}
}
}
console.log("chars", chars);
return output;
}
const output = formater("5F3Z-2e-9-w", 4)
console.log("output", output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment