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

449
Views
Code wars: Why does this function work against whitespace and numbers?

I need to check if two strings are in the same case. I have my own solution to this problem but I found this version and I can't understand how/why it works:

function sameCase(a, b){
 if(a.toUpperCase() === a.toLowerCase() || b.toLowerCase() === b.toUpperCase()){
    return -1
  }else if(a === a.toLowerCase() && b === b.toLowerCase() || a === a.toUpperCase() && b === b.toUpperCase()){
      return 1
  }else{
    return 0
  }
}

Specifically this if statement:

if(a.toUpperCase() === a.toLowerCase() || b.toLowerCase() === b.toUpperCase()){ return -1}

I know this check works against numeric characters and white space, but I don't understand why this works since it doesn't look to take these items into account.

my question: why does this function work against non-letters and numbers and even white space?

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

0

The non-letters are not modified by toUpperCase or toLowerCase. Try picturing a lowercase 9 or . That's because all those functions do is replace the individual letter characters with their uppercase or lowercase equivalents.

So, that code segment just basically checks if the string is entirely composed of non-letter characters (because 924 " is the same in both uppercase or lowercase, however a123 is not the same in both uppercase and lowercase and thus will fail that if statement).

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!