Skip to content

[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

MiguelFerragut
Copy link

No description provided.

Copy link

@0xThales 0xThales left a 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

Comment on lines +6 to +8
} else {
return num2
}

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"

Comment on lines +21 to +23
if (words.length === 0) {
return null
}

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 = []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

unique.push(element);
}

return unique

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])) {

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ía if (!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])) {

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

retorna 0, no null

@MiguelFerragut
Copy link
Author

MiguelFerragut commented Jan 23, 2023 via email

@stale
Copy link

stale bot commented Mar 25, 2023

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
@stale
Copy link

stale bot commented May 31, 2023

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.

@stale stale bot added the stale label May 31, 2023
@stale
Copy link

stale bot commented Jun 7, 2023

This pull request is closed. Thank you.

@stale stale bot closed this Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants