Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
problemas para comprender cómo detener el bucle si es cierto

es mi primera publicación aquí y estoy tratando de hacer un ejercicio de freecodecamp.

Aquí está mi código:

 function mutation(arr) { let newArr = arr[0].toLowerCase(); let arrNew = arr[1].toLowerCase(); let test = newArr.split(' '); let newTest = arrNew.split(' '); for (let i=0; i < test.length; i++) { for (let j=0; j < newTest.length; j++) { if (newTest[j] === test[i]) { return true; } } } return false; } mutation(["hello", "hey"]);

El ejercicio es "Mutaciones" y necesito devolver verdadero si la primera cadena en la matriz tiene todas las letras de la segunda. Estoy tratando de entender cómo detener el ciclo cuando la declaración if es verdadera y guardarla.

Gracias de antemano y perdón si no me expliqué correctamente.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

para tu pregunta

return saldrá de la función y, por lo tanto, saldrá del bucle.
Por eso sabemos que si su función devuelve falso , la condición newTest[j] === test[i] nunca se verificó.

Sugerencia para el ejercicio

Puedes suponer que la primera palabra tiene todas las letras de la segunda, repite como lo estás haciendo ahora y comprueba si encuentras un contraejemplo.

about 4 years ago · Juan Pablo Isaza Relatório

0

El siguiente código debería ayudarte.

 function mutation(arr) { let newArr = arr[0].toLowerCase(); let arrNew = arr[1].toLowerCase(); for (let i=0; i < newArr.length; i++) { if (arrNew.indexOf(newArr[i]) == -1) { return false; } } return true; } mutation(["hello", "hey"]);

No hay necesidad de usar arreglos. en JS, las cadenas funcionan como matrices y podemos usar un bucle "for".

about 4 years ago · Juan Pablo Isaza Relatório

0

Si desea utilizar bucles como en su código de ejemplo, aquí hay una forma muy detallada de hacerlo:

 function mutation(arr) { let hasAllLetters = true; let stringToCompare = arr[0].toLowerCase(); let referenceString = arr[1].toLowerCase(); let hasCurrentLetter; for (let i=0; i < referenceString.length; i++) { hasCurrentLetter = false; for (let j=0; j < stringToCompare.length; j++) { if (referenceString[i] === stringToCompare[j]) { hasCurrentLetter = true; // break from the second loop as soon as the condition is satisfied break; } } if(!hasCurrentLetter){ hasAllLetters = false; // as soon as there is at least one letter which does not exist from the reference string // break from the second loop and exit yielding the result false break; } } return hasAllLetters; } let res; res = mutation(["hello", "goll"]); console.log("res: ", res); // false res = mutation(["hello", "olhh"]); console.log("res: ", res); // true

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda