Last active
May 29, 2017 16:07
-
-
Save rodpoblete/b1f09d0bbe5a4614b6158bba880b6c9a to your computer and use it in GitHub Desktop.
Ejercicio para recortar un cadena, dada la cadena y el limite - FCC [250]
This file contains hidden or 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
function truncateString(str, num) { | |
var dot = '.'; // asigna el punto a una variable String | |
var slice1 = str.slice(0, num) + dot.repeat(3); // primer valor a devolver variable recortada + los 3 puntos suman el limite de num | |
var slice2 = str.slice(0, (num - 3)) + dot.repeat(3); // segundo valor a devolver variable recortada + 3 puntos que no suman el limite de num | |
if (str.length > num && num <= 3) // si el largo de la variable es mayor al limite y el limire menor o igual a 2 | |
return console.log(slice1); // devuelve primera cadena que no considera los 3 puntos como suma del total de caracteres | |
else if (str.length > num && num > 3) // condicion opusta , si largo de string es mayor a limite y limite mayor a 3 | |
return console.log(slice2); // devuelve segunda cadena, que recorta el caracter por el limite y no se suman los 3 puntos como parte del string final | |
else | |
return console.log(str); | |
} | |
truncateString("A-tisket a-tasket A green and yellow basket".length + 2); // devuelve "A-tisket a-taslket A green and yellow basket" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment