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

77
Vistas
how to loop through two indexes in javascript

why it's giving me the first index and i need the ouput below which is "StOp mAkInG SpOnGeBoB MeMeS!"

 function spongeMeme(sentence) {
  let x = sentence.split("");
  for(let i = 0; i < sentence.length;i+=2){
    return x[i].toUpperCase()
  }
}
console.log(spongeMeme("stop Making spongebob Memes!")) // S

// output : StOp mAkInG SpOnGeBoB MeMeS!"
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Try this:

        function spongeMeme(sentence) {
        let x = sentence.split("");
        let newSentence = '';
        for(let i = 0; i < sentence.length;i++){
            // If the letter is even
            if(i % 2 ==0) {
                newSentence += x[i].toUpperCase();
            } 
            // If the letter is odd
            else {
                newSentence += x[i].toLowerCase();
            }
        }
        return newSentence
      }
      console.log(spongeMeme("stop Making spongebob Memes!"))

First of all you can only return once per function, so you have to create a variable and store all the new letter inside, and, at the end, return this variable.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You're returning just the first letter of what is stored inside of x. Try to debug it and see what is going on.

The way I'd do it is like so:

function spongeMeme(sentence) {
return sentence.split("")
.map((s, i) => (i % 2 != 0 ? s.toUpperCase() : s))
.join("");
}
console.log(spongeMeme("stop Making spongebob Memes!"));
about 4 years ago · Juan Pablo Isaza Denunciar

0

You're immediately returning on the first iteration of your for loop, hence the reason you're only getting back a single letter.

Let's start by fixing your existing code:

function spongeMeme(sentence) {
  let x = sentence.split("");

  for(let i = 0; i < sentence.length; i+=2) {

    // You can't return here. Update the value instead.
    return x[i].toUpperCase()
  }

  // Join your sentence back together before returning.
  return x.join("");
}

console.log(spongeMeme("stop Making spongebob Memes!"))

That'll work, but we can clean it up. How? By making it more declarative with map.

function spongeMeme(sentence) {
  return sentence
    .split('') // convert sentence to an array
    .map((char, i) => { // update every other value
      return (i % 2 === 0) ? char.toUpperCase() : char.toLowerCase(); 
    })
    .join(''); // convert back to a string
}
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