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

96
Vistas
JavaScript for loop that alternates two constants

I've been trying to use a for loop to make a code that alternates between two strings and ends with .toUpperCase , but I'm completely stuck. I'm "able" to do it with an array with two strings (and even so it has a mistake, as it ends with the first string of the array...), but not with two separate constants.
Could anyone offer some help?

function repeteFrases(num) {
  const frases = ["frase um", "frase dois"];
  let result = "";
  for (let i = 0; i < num; i++) {
    result += i === num - 1 ? frases[0].toUpperCase() : `${frases[0]}, ${frases[1]}, `;
  }
  return result;
}
console.log(repeteFrases(2));

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

0

In order to alternate between two states you can use the parity of the index, i.e., the condition would be i % 2 == 0, like this:

   function repeteFrases(num) {
     const frases = ["frase um", "frase dois"];
     let result = "";
     for (let i = 0; i < num; i++) {
       result += i % 2 == 0 ? frases[0].toUpperCase() : `${frases[0]}, ${frases[1]}, `;
     }
     return result;
   }
   console.log(repeteFrases(5));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use frases[i % frases.length] to alternate the phrases (however many there might be)

function repeteFrases(num) {
  const frases = ["frase um", "frase dois"];
  let result = "";
  for (let i = 0; i < num; i++) {
    const next = frases[i % frases.length];
    const isLast = (i === num - 1);
    result += isLast ? next.toUpperCase() : `${next}, `;
  }
  return result;
}

console.log(repeteFrases(5));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You've almost got it.. I think what you're asking for is to repeat phrase one or two (alternating), and for the last version to be uppercase. I notice someone else made your code executable, so I might be misunderstanding. We can test odd/even using the modulus operator (%), which keeps the access of frases to either the 0 or 1 position. The other trick is to loop until one less phrase than needed, and append the last one as upper case.

function repeteFrases(num) {
  const frases = ["frase um", "frase dois"];
  //Only loop to 1 less than the expected number of phrases
  const n=num-1;

  let result = "";
  for (let i = 0; i < n; i++) {
    //Note %2 means the result can only be 0 or 1
    //Note ', ' is a guess at inter-phrase padding
    result+=frases[i%2] + ', ';
  }
  //Append last as upper case
  return result+frases[n%2].toUpperCase();
}
console.log(repeteFrases(2));
console.log(repeteFrases(3));

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