2

Is there a way to pass case sensitivity together with an argument?

   var user = {
  'name' : 'Jim',
  'lastName' : 'Xydas',
  'fullName' : function(){
    return this.name + " " + this.lastName
  },

  'address' : {
    'country' : "Greece",
    'town'    : "Thessaloniki",
    'street'  : "il.ap 15",

    "isFromTown" : function(n){
        return this.town == n ? true : false;
    }
  }
};

var checkUser = user.address.isFromTown("thessaloniki");

console.log(checkUser);

What I'm trying to do here is pass the argument as "thessaloniki" and print true, ignoring the first (or more) uppercase letter(s).

https://jsfiddle.net/DimitriXd4/mfpat7so/

Thanks in advance.

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
Dimitris Xydas
  • 233
  • 2
  • 5
  • 9

3 Answers3

3

You could use String#toLowerCase() or String#toLocaleLowerCase for both strings for comparing.

var user = {
    'name' : 'Jim',
    'lastName' : 'Xydas',
    'fullName' : function() {
        return this.name + " " + this.lastName
     },
    'adress' : {
        'country' : "Greece",
        'town'    : "Thessaloniki",
        'street'  : "il.ap 15",
        "isFromTown" : function(n){
            return this.town.toLocaleLowerCase() == n.toLocaleLowerCase();
        }
    }
};

var checkUser = user.adress.isFromTown("thessaloniki");

console.log(checkUser);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
3

Sure, just pass another parameter

"isFromTown" : function(n, caseSensitive){
    return caseSensitive 
      ? this.town == n 
      : this.town.toLowerCase() == n.toLowerCase();
}

https://jsfiddle.net/mfpat7so/1/

Jamiec
  • 133,658
  • 13
  • 134
  • 193
2

Use the below snippet in the function

return (this.town.toUpperCase()) === (n.toUpperCase());
Kushagara.K
  • 67
  • 2
  • 8