|
| 1 | + |
| 2 | +/* |
| 3 | + * Crea 3 funciones, cada una encargada de detectar si una cadena de |
| 4 | + * texto es un heterograma, un isograma o un pangrama. |
| 5 | + * - Debes buscar la definición de cada uno de estos términos. |
| 6 | + */ |
| 7 | + |
| 8 | +let word = "brickfade"; |
| 9 | + |
| 10 | +function isHeterogram(text) { |
| 11 | + //tenemos que eliminar los espacios en blanco |
| 12 | + text = text.toLowerCase().replace(/[^a-z]/g, ''); |
| 13 | + for (let i = 0; i < text.length; i++) { |
| 14 | + for (let j = i + 1; j < text.length; j++) { |
| 15 | + if (text[i] === text[j]) { |
| 16 | + return false; |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | + return true; |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +function isIsogram(text) { |
| 25 | + //tenemos que eliminar los espacios en blanco porque for y set lo cuentan |
| 26 | + text = text.toLowerCase().replace(/[^a-z]/g, ''); |
| 27 | + let referenceCount = 1; |
| 28 | + const lettersSet = new Set(); |
| 29 | + for (let i = 0; i < text.length; i++) { |
| 30 | + if (lettersSet.has(text[i])) { |
| 31 | + continue; |
| 32 | + } else { |
| 33 | + lettersSet.add(text[i]); |
| 34 | + } |
| 35 | + let repeatedCount = 1; |
| 36 | + for (let j = i + 1; j < text.length; j++) { |
| 37 | + if (text[i] === text[j]) { |
| 38 | + if (i === 0) { |
| 39 | + referenceCount++; |
| 40 | + } else { |
| 41 | + repeatedCount++; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + if (i > 0 && referenceCount != repeatedCount) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + } |
| 49 | + return true; |
| 50 | +} |
| 51 | + |
| 52 | +function isPangram(text) { |
| 53 | + let alphabet = 'abcdefghijklmnopqrstuvwxyz'; |
| 54 | + let alphabet_size = alphabet.length; |
| 55 | + if (text.length >= alphabet_size) { |
| 56 | + const alphabet_set = new Set((text.toLowerCase()).replace(/[^a-z]/g, '')); |
| 57 | + // console.log(alphabet_set); |
| 58 | + // console.log(alphabet_set.size); |
| 59 | + if (alphabet_set.size === alphabet_size) { |
| 60 | + return true; |
| 61 | + } |
| 62 | + } |
| 63 | + return false; |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +console.log(isHeterogram(word) ? "es un heterograma" : "no es heterograma"); |
| 68 | +console.log(isIsogram(word) ? "es un isograma" : "no es isograma"); |
| 69 | +console.log(isPangram(word) ? "es un pangrama" : "no es pangrama"); |
| 70 | + |
| 71 | + |
| 72 | + |
0 commit comments