Skip to content

Commit 501f3f7

Browse files
committed
#4 - JavaScript extra exercise.
1 parent facd0c3 commit 501f3f7

File tree

1 file changed

+52
-16
lines changed
  • Roadmap/04 - CADENAS DE CARACTERES/javascript

1 file changed

+52
-16
lines changed

Roadmap/04 - CADENAS DE CARACTERES/javascript/DobleDJ.js

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,12 @@ for (let index = 0; index < message.length; index++) {
109109
const reverseManual = (word) => word.split("").reverse().join("")
110110

111111
/**
112-
* Determine if two words are palindromes, anagram or isogram
113-
* @param {string} firstWord Any string word
114-
* @param {string} secondWord Any string word
115-
* @returns Returns a boolean response true or false
112+
* Extrae las palabras únicas de un string
113+
* @param {string} word Any string word
116114
*/
117-
function textAnalizer(firstWord, secondWord) {
118-
//TODO
119-
if (palindromeTester(firstWord, secondWord)) console.log("Las palabras son palíndromes")
120-
121-
const resultAnagram = anagramaTester(firstWord, secondWord)
122-
const resultIsogram = isogramaTester(firstWord, secondWord)
115+
const palabrasUnicas = (word) => {
116+
//TODO al string realizar .split agregarlo a un Set retornarlo a string y si es igual no tiene palabras repetidas
117+
return Array.from(new Set(word.split(""))).join("")
123118
}
124119

125120
/**
@@ -134,17 +129,58 @@ const palindromeTester = (firstWord, secondWord) => {
134129
}
135130

136131
/**
137-
* Determine if two words are anagram
132+
* Determine if a word is an isogram
133+
* @param {string} word Any string word
134+
* @returns {boolean} Returns true if the word is an isogram, false otherwise
135+
*/
136+
const esIsograma = (word) => {
137+
return word === palabrasUnicas(word)
138+
}
139+
140+
/**
141+
* Determine if two words are isograms
138142
* @param {string} firstWord Any string word
139143
* @param {string} secondWord Any string word
140-
* @returns Returns a boolean response true or false
144+
* @returns {boolean} Returns true if both words are isograms, false otherwise
141145
*/
142-
const anagramaTester = (firstWord, secondWord) => {}
146+
const isogramaTester = (firstWord, secondWord) => {
147+
const firstIsIsogram = esIsograma(firstWord)
148+
const secondIsIsogram = esIsograma(secondWord)
149+
150+
console.log(`La palabra ${firstWord} ${firstIsIsogram ? "es" : "no es"} un isograma`)
151+
console.log(`La palabra ${secondWord} ${secondIsIsogram ? "es" : "no es"} un isograma`)
152+
153+
return firstIsIsogram && secondIsIsogram
154+
}
143155

144156
/**
145-
* Determine if two words are isogram
157+
* Determine if two words are anagrams
146158
* @param {string} firstWord Any string word
147159
* @param {string} secondWord Any string word
148-
* @returns Returns a boolean response true or false
160+
* @returns {boolean} Returns true if both words are anagrams, false otherwise
149161
*/
150-
const isogramaTester = (firstWord, secondWord) => {}
162+
const anagramaTester = (firstWord, secondWord) => {
163+
const normalize = (word) => word.toLowerCase().replace(/\s+/g, "").split("").sort().join("")
164+
return normalize(firstWord) === normalize(secondWord)
165+
}
166+
167+
function textAnalizer(firstWord, secondWord) {
168+
const firstIsIsogram = esIsograma(firstWord)
169+
const secondIsIsogram = esIsograma(secondWord)
170+
171+
if (palindromeTester(firstWord, secondWord)) {
172+
console.log("Las palabras son palíndromes")
173+
} else {
174+
console.log("Las palabras no son palíndromes")
175+
}
176+
177+
if (anagramaTester(firstWord, secondWord)) {
178+
console.log("Las palabras son anagramas")
179+
} else {
180+
console.log("Las palabras no son anagramas")
181+
}
182+
console.log(`La palabra ${firstWord} ${firstIsIsogram ? "es" : "no es"} un isograma`)
183+
console.log(`La palabra ${secondWord} ${secondIsIsogram ? "es" : "no es"} un isograma`)
184+
}
185+
186+
textAnalizer("Listen", "silent")

0 commit comments

Comments
 (0)