|
| 1 | +// Autor: EdiedRamos |
| 2 | + |
| 3 | +// Ejercicio base |
| 4 | +function stringFunctions() { |
| 5 | + const username: string = "Edied Ramos"; |
| 6 | + const dirtyUsername: string = " Edied Ramos "; |
| 7 | + |
| 8 | + console.log("Longitud de la cadena:", username.length); |
| 9 | + console.log("Carácter en la primer posición:", username.charAt(0)); |
| 10 | + console.log( |
| 11 | + "Carácter en la primer posición pero en código UTF-16:", |
| 12 | + username.charCodeAt(0) |
| 13 | + ); |
| 14 | + |
| 15 | + console.log("Carácter en la última posición:", username.at(-1)); |
| 16 | + console.log("Últimos 5 carácteres:", username.slice(-5)); |
| 17 | + console.log("Primeros 5 carácteres:", username.substring(0, 5)); |
| 18 | + // El método "substr" está deprecado, pero es útil para compatibilidad |
| 19 | + console.log( |
| 20 | + "4 carácteres a partír de la segunda posición:", |
| 21 | + username.substr(1, 4) |
| 22 | + ); |
| 23 | + |
| 24 | + console.log("Nombre de usuario en mayúscula:", username.toUpperCase()); |
| 25 | + console.log("Nombre de usuario en minuscula:", username.toLowerCase()); |
| 26 | + |
| 27 | + console.log( |
| 28 | + "Saludo concatenado con el nombre de usuario:", |
| 29 | + "Hola ".concat(username) |
| 30 | + ); |
| 31 | + |
| 32 | + console.log( |
| 33 | + "Nombre de usuario sin espacios al inicio ni al final:", |
| 34 | + dirtyUsername.trim() |
| 35 | + ); |
| 36 | + console.log( |
| 37 | + "Nombre de usuario sin espacios al inicio:", |
| 38 | + dirtyUsername.trimStart() |
| 39 | + ); |
| 40 | + console.log( |
| 41 | + "Nombre de usuario sin espacios al final:", |
| 42 | + dirtyUsername.trimEnd() |
| 43 | + ); |
| 44 | + |
| 45 | + console.log(" con 15 carácteres:", username.padStart(15, "_")); |
| 46 | + console.log("Nombre de usuario con 15 carácteres:", username.padEnd(15, "_")); |
| 47 | + |
| 48 | + console.log("2 veces el nombre de usuario:", username.repeat(2)); |
| 49 | + |
| 50 | + console.log("Reemplaza la primer d por D:", username.replace("d", "D")); |
| 51 | + console.log("Reemplaza todas las d por D:", username.replaceAll("d", "D")); |
| 52 | + |
| 53 | + console.log("Palabras en el nombre de usuario: ", username.split(" ")); |
| 54 | +} |
| 55 | + |
| 56 | +// Ejercicio extra |
| 57 | +function createMessage( |
| 58 | + value: string, |
| 59 | + status: boolean, |
| 60 | + context: string, |
| 61 | + isPlural?: boolean |
| 62 | +) { |
| 63 | + return `${value}: ${ |
| 64 | + status ? (isPlural ? "Son" : "Es") : isPlural ? "No son " : "No es" |
| 65 | + } ${context}`; |
| 66 | +} |
| 67 | + |
| 68 | +function palindromeCase() { |
| 69 | + const isPalindrome = (str: string): boolean => { |
| 70 | + for (let i = 0; i < str.length / 2; i++) { |
| 71 | + if (str[i] !== str[str.length - i - 1]) return false; |
| 72 | + } |
| 73 | + return true; |
| 74 | + }; |
| 75 | + const firstWord = "abcba", |
| 76 | + secondWord = "ediedramos"; |
| 77 | + |
| 78 | + console.log(createMessage(firstWord, isPalindrome(firstWord), "palindromo")); |
| 79 | + console.log( |
| 80 | + createMessage(secondWord, isPalindrome(secondWord), "palindromo") |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +function anagramCase() { |
| 85 | + const areAnagram = (wordA: string, wordB: string): boolean => { |
| 86 | + const wordASorted = [...wordA].sort().join(""); |
| 87 | + const wordBSorted = [...wordB].sort().join(""); |
| 88 | + return wordASorted === wordBSorted; |
| 89 | + }; |
| 90 | + const firstWord = "ramosedied", |
| 91 | + secondWord = "ediedramos"; |
| 92 | + |
| 93 | + console.log( |
| 94 | + createMessage( |
| 95 | + `${firstWord} y ${secondWord}`, |
| 96 | + areAnagram(firstWord, secondWord), |
| 97 | + "anagramas", |
| 98 | + true |
| 99 | + ) |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +function isogramCase() { |
| 104 | + const isIsogram = (str: string): boolean => { |
| 105 | + const frecuency: Record<number, number> = {}; |
| 106 | + for (const char of str) { |
| 107 | + const isALetter = /[a-z]/i.test(char); |
| 108 | + if (isALetter) { |
| 109 | + if (!frecuency[char]) frecuency[char] = 0; |
| 110 | + frecuency[char]++; |
| 111 | + } |
| 112 | + } |
| 113 | + const unique = new Set(); |
| 114 | + for (const key of Object.keys(frecuency)) unique.add(frecuency[key]); |
| 115 | + return unique.size === 1; |
| 116 | + }; |
| 117 | + const firstWord = "edied", |
| 118 | + secondWord = "ramos"; |
| 119 | + |
| 120 | + console.log(createMessage(firstWord, isIsogram(firstWord), "isograma")); |
| 121 | + console.log(createMessage(secondWord, isIsogram(secondWord), "isograma")); |
| 122 | +} |
| 123 | + |
| 124 | +function extraExercise() { |
| 125 | + palindromeCase(); |
| 126 | + anagramCase(); |
| 127 | + isogramCase(); |
| 128 | +} |
| 129 | + |
| 130 | +(() => { |
| 131 | + stringFunctions(); |
| 132 | + extraExercise(); |
| 133 | +})(); |
0 commit comments