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

81
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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