JSFiddle - React, Tailwind, and code Playground

by serGlazkov

JavaScript

const capitalize = sentence => {
  let wordsArray = sentence.split(' ');

  const transform = word => {
    return word.replace(/[a-z]/gi, (letter, position) => letter[position % 2 === 1 ? 'toLowerCase' : 'toUpperCase']());
  }

  return wordsArray.map(transform).join(' ');
}

console.log(capitalize('hello world  testing  123'));
console.log(capitalize(' I will see you in 2wo years '));