Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

76
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!