DEV Community

Chinwendu Agbaetuo
Chinwendu Agbaetuo

Posted on

1 1 1 1 1

Reverse the case of a string - Javascript

Write a function that takes a string and reverses the case of all its letters.

Solution

function reverseStringCase(text) {
  let reversedString = [];

  for (let i = 0; i < text.length; i++) {
    if (text[i] == text[i].toUpperCase()) {
      reversedString.push(text[i].toLowerCase());
    } else {
      reversedString.push(text[i].toUpperCase());
    }
  }

  console.log(reversedString.join(""));
  return reversedString.join("");
}

// Another solution for reversing string cases

function reverseStringCases(text) {
  const reversedString = text
    .split("")
    .map((item) =>
      item === item.toUpperCase() ? item.toLowerCase() : item.toUpperCase()
    )
    .join("");

  console.log(reversedString);
  return reversedString;
}

reverseStringCase(
  "L'âme nE se dÉvelopPe pAs sAnS ChAngEmenT, eT leS DoUtes Sont EsSentIels à la crOisSanCe."
);

reverseStringCases(
  "L'âme nE se dÉvelopPe pAs sAnS ChAngEmenT, eT leS DoUtes Sont EsSentIels à la crOisSanCe."
);
Enter fullscreen mode Exit fullscreen mode

Result

l'ÂME Ne SE DéVELOPpE PaS SaNs cHaNGeMENt, Et LEs dOuTES sONT eSsENTiELS À LA CRoISsANcE.

l'ÂME Ne SE DéVELOPpE PaS SaNs cHaNGeMENt, Et LEs dOuTES sONT eSsENTiELS À LA CRoISsANcE.
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (1)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more