- Notifications
You must be signed in to change notification settings - Fork6.7k
[MAD de 0123]Miguel Ferragut#3569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
0xThales left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Un lab un poco más complicado.
Te he dejado unos cuantos comentarios para solucionar algunos problemas y mejorar tu código.
Dale caña, esto es cuestión de enfrentarse al mismo problema cientos de veces. Cualquier cosa, me comentas. Un abrazo
} else { | ||
return num2 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Te sobra el else. Puede hacer directamente "return num2"
if (words.length === 0) { | ||
return null | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Dado que 0 es un valor falsy, este condicional podría refactorizarse de esta manera (más elegante y común en la industria):
if (!words.length) { return null }
Por otra parte, cuando el cuerpo de un condicional solo contiene una expresión, no es necesario servirse de bloques. Podemos refactorizar esa parte así (aplicable a cualquier parte del código):
if (!words.length) return null
Así queda un poco más limpio y organizado.
// Iteration #2: Find longest word | ||
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; | ||
function findLongestWord() {} | ||
let longest = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Gran problema aquí!!!
Si declaras la variable fuera de tu función, cuando el test se ejecuta no tiene acceso a esta... (Si vas al final del archivo verás que ahí se encuentran las exportaciones de tus funciones. En jest esas funciones se importan y se ejecutan pasando varios tests. Esos test no tienen acceso a NADA que no esté definido dentro de las funciones
Es mejor definir la variable como una string vacía, tiene más sentido, ya que vas a sustituir el valor de esta por nuevas strings. Si no, das a entender que vas a usar ese array para algo. Simplemente define añade
let longest = ""
dentro de tu función y ya estaría resuelto todo.
unique.push(element); | ||
} | ||
return unique |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Este return está dentro de tu for...
Tienes que pasarlo una línea más abajo.
let element = wordsUnique[i]; | ||
if (!unique.includes(element[i])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Aquí está el otro problema. Seríaif (!unique.includes(element))
"element" ya es wordsUnique[i]
for (let i = 0; i < wordsCount.length; i++) { | ||
let word = wordsCount[i] | ||
if (word.includes(wordsCount[i])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
wordsCount es tu array de palabras, no "word".
A la vez, includes solo busca la primera instancia de un elemento. Este método no te sirve para esta iteración.
Simplemente podrías comprobar la identidad estricta del elemento con "word". Si es el caso, entonces sumas 1. Una posible solución:
function howManyTimes(words, word) { let times = 0 if (!words.length) { return 0 } for (e of words) { if (e === word) times++ } return times}
let times = 0 | ||
if (wordsCount.length === 0) { | ||
return null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
retorna 0, no null
Si que hice mal este,si...muchas gracias!El vie, 20 ene 2023 a las 12:08, Daniel Jimenez ***@***.***>)escribió: … ***@***.**** commented on this pull request. Un lab un poco más complicado. Te he dejado unos cuantos comentarios para solucionar algunos problemas y mejorar tu código. Dale caña, esto es cuestión de enfrentarse al mismo problema cientos de veces. Cualquier cosa, me comentas. Un abrazo ------------------------------ In src/functions-and-arrays.js <#3569 (comment)> : > + } else { + return num2 + } Te sobra el else. Puede hacer directamente "return num2" ------------------------------ In src/functions-and-arrays.js <#3569 (comment)> : > + if (words.length === 0) { + return null + } Dado que 0 es un valor falsy, este condicional podría refactorizarse de esta manera (más elegante y común en la industria): if (!words.length) { return null } Por otra parte, cuando el cuerpo de un condicional solo contiene una expresión, no es necesario servirse de bloques. Podemos refactorizar esa parte así (aplicable a cualquier parte del código): if (!words.length) return null Así queda un poco más limpio y organizado. ------------------------------ In src/functions-and-arrays.js <#3569 (comment)> : > // Iteration#2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +let longest = [] Gran problema aquí!!! 1. Si declaras la variable fuera de tu función, cuando el test se ejecuta no tiene acceso a esta... (Si vas al final del archivo verás que ahí se encuentran las exportaciones de tus funciones. En jest esas funciones se importan y se ejecutan pasando varios tests. Esos test no tienen acceso a NADA que no esté definido dentro de las funciones 2. Es mejor definir la variable como una string vacía, tiene más sentido, ya que vas a sustituir el valor de esta por nuevas strings. Si no, das a entender que vas a usar ese array para algo. Simplemente define añade let longest = "" dentro de tu función y ya estaría resuelto todo. ------------------------------ In src/functions-and-arrays.js <#3569 (comment)> : > + + let unique = [] + + if (wordsUnique.length === 0) { + return null + } + + for (let i = 0; i < wordsUnique.length; i++) { + + let element = wordsUnique[i]; + + if (!unique.includes(element[i])) { + unique.push(element); + } + + return unique Este return está dentro de tu for... Tienes que pasarlo una línea más abajo. ------------------------------ In src/functions-and-arrays.js <#3569 (comment)> : > @@ -52,14 +121,48 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(wordsUnique) { + + let unique = [] + + if (wordsUnique.length === 0) { + return null + } + + for (let i = 0; i < wordsUnique.length; i++) { + + let element = wordsUnique[i]; + + if (!unique.includes(element[i])) { Aquí está el otro problema. Sería if (!unique.includes(element)) "element" ya es wordsUnique[i] ------------------------------ In src/functions-and-arrays.js <#3569 (comment)> : > @@ -78,7 +181,23 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(wordsCount) { + + let times = 0 + + if (wordsCount.length === 0) { + return null + } + + for (let i = 0; i < wordsCount.length; i++) { + let word = wordsCount[i] + + if (word.includes(wordsCount[i])) { wordsCount es tu array de palabras, no "word". A la vez, includes solo busca la primera instancia de un elemento. Este método no te sirve para esta iteración. Simplemente podrías comprobar la identidad estricta del elemento con "word". Si es el caso, entonces sumas 1. Una posible solución: function howManyTimes(words, word) { let times = 0 if (!words.length) { return 0 } for (e of words) { if (e === word) times++ } return times } ------------------------------ In src/functions-and-arrays.js <#3569 (comment)> : > @@ -78,7 +181,23 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(wordsCount) { + + let times = 0 + + if (wordsCount.length === 0) { + return null retorna 0, no null — Reply to this email directly, view it on GitHub <#3569 (review)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/A5EOPB5FZNMY5MXKQOAEXLTWTJWZTANCNFSM6AAAAAAUAYR6TE> . You are receiving this because you authored the thread.Message ID: <ironhack-labs/lab-javascript-functions-and-arrays/pull/3569/review/1263313684 @github.com> |
This pull request has been automatically marked as stale because it didn't have any recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
1 similar comment
This pull request has been automatically marked as stale because it didn't have any recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This pull request is closed. Thank you. |
No description provided.