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

315
Views
(Javascript) Make every second word in a sentence uppercase

As the title says, i need to make a function that would make every second word in a sentence uppercase, but do not touch other words. I have this function : (The first if is in a case there is only one word)

And it does its job, however with a sentence like this "ThIs iS A seNteNCe" it returns "this IS a SENTENCE", whereas i need "ThIs IS a SENTENCE".

function largeLetters(sentence) {
  return sentence.map((v, i) => i % 2 == 0 ? v.toLowerCase() : v.toUpperCase()).join(' ');
}

function sentenceToUpperCase(sentence) {
  sentence = sentence.split(' ');
  if (sentence.length == 1) {
    sentence = sentence.toString().toUpperCase();
    return sentence;
  } else {
    return largeLetters(sentence)
  };
};

console.log(sentenceToUpperCase("ThIs iS A seNteNCe"))

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I've simplified your program a bit.

The problem was in this part:

i % 2 == 0 ? word.toLowerCase() : word.toUpperCase()

you don't need to change every other word to lowercase, you just leave it as it is:

i % 2 == 0 ? word : word.toUpperCase()

function sentenceToUpperCase(sentence) {
 return sentence
   .split(' ')
   .map((word, i) => i % 2 == 0 ? word : word.toUpperCase())
   .join(' ');
};
console.log(sentenceToUpperCase("ThIs"))
console.log(sentenceToUpperCase("ThIs iS"))
console.log(sentenceToUpperCase("ThIs iS A"))
console.log(sentenceToUpperCase("ThIs iS A seNteNCe"))

about 4 years ago · Juan Pablo Isaza Report

0

When you say

that would make every second word in a sentence uppercase

If you mean 'even value' so use value % 2 === 0 else just change the 2nd value of your array.

Update even words :

function sentenceToUpperCase(sentence) {
  const words = sentence.split(' ');
  if(words.length >= 2) {
    return words.map((v,i) => i % 2 === 0 ? v : v.toUpperCase()).join(' ');
  } else { 
    return sentence.toUpperCase();
  }
}

Only update 2nd word :

function sentenceToUpperCase(sentence) {
  const words = sentence.split(' ');
  if(words.length >= 2) {
    words[1] = words[1].toUpperCase();
  } else { 
    return sentence.toUpperCase();
  }
  return words.join(' ');
}

You can 'minify' it by using a ternary and map

function sentenceToUpperCaseMinify(sentence) {
  const words = sentence.split(' ');
  return words.length >= 2 ? words.map((v, i) => i === 1 ? v.toUpperCase() : v).join(' ') : sentence.toUpperCase();
}
about 4 years ago · Juan Pablo Isaza Report

0

Cool question and in JS you have many ways to do that. The functions I have used are: lowercase, split, toUpperCase, join. I wrapped in a function.

const str = "this IS a SENTENCE"

function build(str) {
  const s = str.split(' ');  
  // or use (str.toLowerCase()).split(' ') if every odd word have to be lowercase
  let r = [];
  let i = 0;

  s.forEach((e) => {
    if (i % 2 === 0) {
      r.push(e)          
    } else {
      r.push(e.toUpperCase())
    }
    i++;
    })
  return r.join(' ');
}

console.log('new Sentense: ', build(str) );

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!