|
| 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 | +const operationString = 'Hello world'; |
| 9 | +const supportString = 'Javascript!'; |
| 10 | + |
| 11 | +console.log('Original string: ' + operationString); |
| 12 | +console.log('Access to second character: ' + operationString[1]); // Accesing an specific character |
| 13 | +console.log('Verifying the existence of a substring: ', operationString.includes('world')) // Finding a substring |
| 14 | +console.log('Getting the substring index: ', operationString.indexOf('world')); // Finding the index of a substring |
| 15 | +console.log('Extracting a substring: ', operationString.slice(-5)); // Extracting a substring |
| 16 | +console.log('String lenght is: ' + operationString.length); // Length of a string |
| 17 | +console.log('Concatenated string: ' + `${operationString} - ${supportString}`); // Concatenating strings |
| 18 | +console.log('Repeat a string: ', supportString.repeat(2)); // Repeating a string |
| 19 | +console.log('Convert to upper case: ', supportString.toUpperCase()); // Converting to upper cas |
| 20 | +console.log('Convert to lower case: ', supportString.toLowerCase()); // Converting to lower case |
| 21 | +console.log('Replacing a substring: ', operationString.replace('world', supportString)); // Replacing a substring with other substring |
| 22 | +console.log('Spliting a string: ', operationString.split(' ')); // Spliting a string |
| 23 | +console.log(`Interpolating a string in ${supportString}`); // Interpolating a string |
| 24 | +console.log('Using scape characters:\n', operationString); // Scape characters |
| 25 | +// Iterating a string |
| 26 | +for (i in supportString) { |
| 27 | + console.log(supportString[i]); |
| 28 | +} |
| 29 | + |
| 30 | +/* |
| 31 | +DIFICULTAD EXTRA (opcional): |
| 32 | +Crea un programa que analice dos palabras diferentes y realice comprobaciones |
| 33 | +para descubrir si son: |
| 34 | +- Palíndromos |
| 35 | +- Anagramas |
| 36 | +- Isogramas |
| 37 | +*/ |
| 38 | + |
| 39 | +function preProcess(userText) { |
| 40 | + let lowerCaseString = userText.toLowerCase(); |
| 41 | + let originalString = lowerCaseString.split(' ').join(''); |
| 42 | + return originalString.split(''); |
| 43 | +} |
| 44 | + |
| 45 | +function isPalindrome(userText) { |
| 46 | + let lowerCaseString = userText.toLowerCase(); |
| 47 | + let originalString = lowerCaseString.split(' ').join(''); |
| 48 | + let splitArray = originalString.split(''); |
| 49 | + let reversedString = splitArray.reverse().join(''); |
| 50 | + |
| 51 | + if (reversedString == originalString) { |
| 52 | + console.log(`The text "${userText}" is palindrome`); |
| 53 | + return true; |
| 54 | + } |
| 55 | + else { |
| 56 | + console.log(`The text "${userText}" is not a palindrome`); |
| 57 | + return false; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function isAnagram(firstText, secondText) { |
| 62 | + let firstTextArray = preProcess(firstText); |
| 63 | + let secondTextArray = preProcess(secondText); |
| 64 | + |
| 65 | + if (firstTextArray.sort().join('') === secondTextArray.sort().join('')) { |
| 66 | + console.log(`The texts "${firstText}" and "${secondText}" are anagrams`); |
| 67 | + return true; |
| 68 | + } |
| 69 | + else { |
| 70 | + console.log(`The texts "${firstText}" and "${secondText}" are not anagrams`); |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +function isIsogram(userText) { |
| 77 | + let userTextArray = preProcess(userText); |
| 78 | + |
| 79 | + let evaluationSet = new Set(); |
| 80 | + userTextArray.forEach(element => { |
| 81 | + evaluationSet.add(element); |
| 82 | + }); |
| 83 | + evaluationArray = Array.from(evaluationSet); |
| 84 | + if (userTextArray.join('') === evaluationArray.join('')) { |
| 85 | + console.log(`The text "${userText}" is an isogram`); |
| 86 | + return true; |
| 87 | + } |
| 88 | + else { |
| 89 | + console.log(`The text "${userText}" is not an isogram`); |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +console.log('----- Palindrome -----') |
| 96 | +isPalindrome('Hello Javascript'); |
| 97 | +isPalindrome('Nurses run'); |
| 98 | +console.log('----- Anagrams -----'); |
| 99 | +isAnagram('Javascript', 'Cinema'); |
| 100 | +isAnagram('iceman', 'Cinema'); |
| 101 | +console.log('----- Isogram -----'); |
| 102 | +isIsogram('Javascript'); |
| 103 | +isIsogram('Lumberjacks'); |
0 commit comments