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

183
Views
Convert Cases in a String

I am trying to create a function to reverse all cases in a string. All lower-case letters should be upper-case, and vice versa. For example: reverseCase("Happy Birthday") ➞ "hAPPY bIRTHDAY"

My code below doesn't work. :(

const functionX = X =>
  X === X.toLowerCase() ? X.toUpperCase() : X.toLowerCase();

function reverseCase(string) {
  const letters = string.split('');
  letters.forEach(functionX);
  return letters.join('');
}

console.log(reverseCase('Happy Birthday!'));

almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The forEach function does not return the output letter even though it executes the functionX for each input letter. You need to use map instead to return the letters.

Try like this:

const functionX = X =>
  X === X.toLowerCase() ? X.toUpperCase() : X.toLowerCase();

function reverseCase(string) {
  const letters = string.split('');
  return letters.map(functionX).join('');
}

console.log(reverseCase('Happy Birthday!'));

almost 4 years ago · Santiago Trujillo Report

0

Hope its clear

function reverseCase(string) {
  const letters = string.split('');
  let resultLetters = letters.map(letter=> {
    let result = letter == letter.toLowerCase() ? letter.toUpperCase() : letter.toLowerCase();
    return result;
  });
  return resultLetters.join('');
}

console.log(reverseCase('Happy Birthday!'));

almost 4 years ago · Santiago Trujillo 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!