Created
May 23, 2017 21:57
-
-
Save rodpoblete/69ad367e9e3423743da3feeef8878dc6 to your computer and use it in GitHub Desktop.
Palindrome Exercise FCC / Unprocessed Algorithm Poor performance :(
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 palindrome(str) { | |
var strLowerCase = str.toLowerCase(); // Convierte cadena en minusculas. | |
var strClean = strLowerCase.replace(/[^\w\s]/gi, ''); // Limpia caracteres del texto. los reemplaza por vacio '' | |
var strSymbols = strClean.replace(/_/g, " "); // limpia el caracter "_" que por alguna razón no lo limpia la funcion anterior. | |
var strSpace = strSymbols.replace(/ /g, ''); // elimina los espacios en blanco. | |
var strReverse = strSpace.split('').reverse().join(''); // convierte el string en array lo reversa y lo une para formar una palabra. | |
if (strReverse !== strSpace) { // comprar el string reversado con el string limpio para ver si son iguales como un palindromor EJ: ojo === ojo | |
return false; // retorna falso si son diferentes, no son palindromos. | |
} else { | |
return true; // retorna verdadero si son iguales, es palindromo :D | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lo quiero depurar. Muchas declaraciones para limpiar strings.
podría ser mas recursivo en las funciones. ☝️