Created
November 30, 2022 11:45
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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