Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
trouble compreending how to stop the loop if true

it's my first post here and I'm trying to do a freecodecamp exercise.

Here's my code:

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"]);

The exercise is "Mutations" and I need to return true if the 1st string on the array has all the letters from the 2nd. I'm trying to understand how to stop the loop when the if statement is true and save it.

Thanks in advance and sorry if I didn't explain myself correctly.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

For your question

return will exit the function and therefore will exit the loop.
From that we know that if your function returns false the condition newTest[j] === test[i] was never verified.

Hint for the exercise

You can suppose that the first word has all the letters from the second, iterate just like you're doing right now and check if you find a counter-example !

about 4 years ago · Juan Pablo Isaza Denunciar

0

The following code should help you.

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"]);

There is no need to use arrays. in JS, strings work like arrays and we can use a "for" loop.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to utilize loops like in your example code here is a very verbose way to do so:

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda