|
| 1 | +/* |
| 2 | + * EJERCICIO: |
| 3 | + * Muestra ejemplos de todas las operaciones que puedes realizar con cadenas de caracteres |
| 4 | + * en tu lenguaje. Algunas de esas operaciones podrían ser (busca todas las que puedas): |
| 5 | + * - Acceso a caracteres específicos, subcadenas, longitud, concatenación, repetición, recorrido, |
| 6 | + * conversión a mayúsculas y minúsculas, reemplazo, división, unión, interpolación, verificación... |
| 7 | + * |
| 8 | + * DIFICULTAD EXTRA (opcional): |
| 9 | + * Crea un programa que analice dos palabras diferentes y realice comprobaciones |
| 10 | + * para descubrir si son: |
| 11 | + * - Palíndromos (se leen igual de izquierda a derecha y de derecha a izquierda) |
| 12 | + * - Anagramas (Las letras de una palabra, si se escriben en orden diferente, forman otra) |
| 13 | + * - Isogramas (Las letras se repiten el mismo número de veces) |
| 14 | + */ |
| 15 | + |
| 16 | +// Sting length |
| 17 | +let text = "ABCDEFGHIJKLLLMNOPQRSTUVWXYZ"; |
| 18 | +let length = text.length; |
| 19 | +console.log("Length of the text: " + length); |
| 20 | + |
| 21 | + |
| 22 | +// Accessing characters |
| 23 | +console.log("Accessing characters"); |
| 24 | +console.log("First character: " + text[0]); |
| 25 | +console.log("Last character: " + text[length - 1]); |
| 26 | +console.log("Character at index 2: " + text[2]); |
| 27 | +console.log("Character at index 10: " + text[10]); |
| 28 | +console.log("Character at index -1: " + text[length + 1]); |
| 29 | + |
| 30 | +// Accessing substrings |
| 31 | +console.log("Accessing substrings"); |
| 32 | +let substring = text.substring(2, 5); |
| 33 | +console.log("Substring from index 2 to index 5: " + substring); |
| 34 | +substring = text.substring(2); |
| 35 | +console.log("Substring from index 2 to the end: " + substring); |
| 36 | +substring = text.substring(0, length); |
| 37 | +console.log("Substring from the beginning to the end: " + substring); |
| 38 | + |
| 39 | +// Concatenation |
| 40 | +console.log("Concatenation"); |
| 41 | +let concatenatedText = text + "123456789"; |
| 42 | +console.log("Concatenated text: " + concatenatedText); |
| 43 | + |
| 44 | +// Repetition |
| 45 | +console.log("Repetition"); |
| 46 | +let repeatedText = text + text; |
| 47 | +console.log("Repeated text: " + repeatedText); |
| 48 | + |
| 49 | +// Iterating over characters |
| 50 | +console.log("Iterating over characters"); |
| 51 | +for (let i = 0; i < length; i++) { |
| 52 | + console.log("Character at index " + i + ": " + text[i]); |
| 53 | +} |
| 54 | + |
| 55 | +// Iterating over substrings |
| 56 | +console.log("Iterating over substrings"); |
| 57 | +for (let i = 0; i < length; i++) { |
| 58 | + let substring = text.substring(i, i + 3); |
| 59 | + console.log("Substring from index " + i + " to index " + (i + 3) + ": " + substring); |
| 60 | +} |
| 61 | + |
| 62 | +// Converting to uppercase |
| 63 | +console.log("Converting to uppercase"); |
| 64 | +let uppercaseText = text.toUpperCase(); |
| 65 | +console.log("Uppercase text: " + uppercaseText); |
| 66 | + |
| 67 | +// Converting to lowercase |
| 68 | +console.log("Converting to lowercase"); |
| 69 | +let lowercaseText = text.toLowerCase(); |
| 70 | +console.log("Lowercase text: " + lowercaseText); |
| 71 | + |
| 72 | +// Replacing characters |
| 73 | +console.log("Replacing characters"); |
| 74 | +let replacedText = text.replace("A", "X"); |
| 75 | +console.log("Replaced text: " + replacedText); |
| 76 | + |
| 77 | +// Splitting a string |
| 78 | +console.log("Splitting a string"); |
| 79 | +let splitText = text.split(""); |
| 80 | +console.log("Split text: " + splitText); |
| 81 | + |
| 82 | +//String Interpolation method: Interpolate variables and expressions into strings |
| 83 | +console.log("Interpolate between M and N the string: INTERPOLATED_TEXT"); |
| 84 | +let interpolateText = `${text.slice(0, text.indexOf('N'))} INTERPOLATED_TEXT ${text.slice(text.indexOf('N'))}`; |
| 85 | +console.log("Interpolate text: " + interpolateText); |
| 86 | + |
| 87 | +// Checking if a string is empty |
| 88 | +console.log("Checking if a string is empty"); |
| 89 | +if (text.length === 0) { |
| 90 | + console.log("The text is empty"); |
| 91 | +} else { |
| 92 | + console.log("The text is not empty"); |
| 93 | +} |
| 94 | + |
| 95 | +// Checking if a string contains a substring |
| 96 | +console.log("Checking if a string contains a substring"); |
| 97 | +if (text.includes("A")) { |
| 98 | + console.log("The text contains the substring 'A'"); |
| 99 | +} else { |
| 100 | + console.log("The text does not contain the substring 'A'"); |
| 101 | +} |
| 102 | + |
| 103 | +// Checking if a string starts with a substring |
| 104 | +console.log("Checking if a string starts with a substring"); |
| 105 | +if (text.startsWith("A")) { |
| 106 | + console.log("The text starts with the substring 'A'"); |
| 107 | +} else { |
| 108 | + console.log("The text does not start with the substring 'A'"); |
| 109 | +} |
| 110 | + |
| 111 | +// Checking if a string ends with a substring |
| 112 | +console.log("Checking if a string ends with a substring"); |
| 113 | +if (text.endsWith("A")) { |
| 114 | + console.log("The text ends with the substring 'A'"); |
| 115 | +} else { |
| 116 | + console.log("The text does not end with the substring 'A'"); |
| 117 | +} |
| 118 | + |
| 119 | +// Checking if a string contains a sequence of characters |
| 120 | +console.log("Checking if a string includes a sequence of characters"); |
| 121 | +if (text.includes("ABCDE")) { |
| 122 | + console.log("true"); |
| 123 | +} else { |
| 124 | + console.log("false"); |
| 125 | +} |
| 126 | + |
| 127 | +console.log("Dificultad EXTRA"); |
| 128 | +/*DIFICULTAD EXTRA (opcional): |
| 129 | + * Crea un programa que analice dos palabras diferentes y realice comprobaciones |
| 130 | + * para descubrir si son: |
| 131 | + * - Palíndromos (se leen igual de izquierda a derecha y de derecha a izquierda) |
| 132 | + * - Anagramas (Las letras de una palabra, si se escriben en orden diferente, forman otra) |
| 133 | + * - Isogramas (Las letras se repiten el mismo número de veces) |
| 134 | + */ |
| 135 | + |
| 136 | + |
| 137 | +console.log("Reverse a string & check if it is a palindrome, anagram or isogram") |
| 138 | +let word1 = "oso"; |
| 139 | +let word2 = "nana"; |
| 140 | + |
| 141 | +let reversedWord = word1.split("").reverse().join(""); |
| 142 | +console.log(`"${word1}" is a palindrome:`, word1 === reversedWord); // you can read the word in reverse |
| 143 | +console.log(`"${word2}" is an anagram of "${word1}":`, word2 === reversedWord);// the letters of a word cab form another word |
| 144 | +console.log(`"${word1}" is an isogram:`, word1.length === new Set(word1).size); // it has no repeated characters |
| 145 | + |
| 146 | + |
0 commit comments