Skip to content

Instantly share code, notes, and snippets.

@elGuille-info
Created November 30, 2022 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elGuille-info/55667988812fac469bf409a6d81592d3 to your computer and use it in GitHub Desktop.
Save elGuille-info/55667988812fac469bf409a6d81592d3 to your computer and use it in GitHub Desktop.
Acumular letras de forma que la primera letra se muestre una vez, la segunda dos veces y así sucesivamente separándolas con un guion (JavaScript)
/*
Acumular letras, de forma que la primera letra se muestre una vez, la segunda 2 veces, etc.
Debe empezar por una letra mayúscula y el resto minúsculas.
Ejemplos:
accum("abcd") -> "A-Bb-Ccc-Dddd"
accum("RqaEzty") -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") -> "C-Ww-Aaa-Tttt"
*/
function accum(s) {
// your code
var nueva = "";
var num = 1;
for (var i = 0; i < s.length; i++) {
if (num == 1) {
nueva = s[i].toUpperCase() + "-";
}
else {
nueva += s[i].toUpperCase();
for(var j = 1; j < num; j++) {
nueva += s[i].toLowerCase();
}
if (i < s.length - 1 ) {
nueva += "-";
}
}
num++;
}
return nueva;
}
console.log("abcd = " + accum("abcd"));
console.log("ZpglnRxqenU = " + accum("ZpglnRxqenU"));
console.log("accum(ZpglnRxqenU) ?= Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu");
if (accum("ZpglnRxqenU") == "Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu") console.log("OK"); else console.log("No OK");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment