?
Solved

JavaScript: Case insensitive test against each item in array

Posted on 2011-02-20
4
Medium Priority
?
805 Views
Last Modified: 2012-05-11
I use the code below to test a string against each item in array.  (Thanks, sjklein42)

Now I want it to be case insensitive.  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<script type="text/javascript">
/*<![CDATA[*/

Array.prototype.contains = function(obj) {
  // Thanks, sjklein42
  var i = this.length;
  while (i--) {
    if (obj.indexOf(this[i].toLowerCase()) != (-1))
    {
      return true;
    }
  }
  return false;
}

var badStuff = new Array("aaa","bbb","ccc","ddd","EEE");

var v = 'AAA hello world';
alert(badStuff.contains(v)); // should alert true because v contains aaa and aaa is in the baddStuff array

var v = 'This is a bbbbbbbb test';
alert(badStuff.contains(v)); // should alert true because v contains bbb and bbb is in the baddStuff array

var v = 'Hello World';
alert(badStuff.contains(v)); // should alert false

var v = 'CCC';
alert(badStuff.contains(v)); // should alert true

/*]]>*/
</script>

</head>
<body>

<h1>Hello World</h1>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 42

Accepted Solution

by:
David S. earned 1600 total points
ID: 34939308
You're close. You just need to make sure that they're both lowercase.
Array.prototype.contains = function(obj) {
  // Thanks, sjklein42
  var i = this.length;
  obj = obj.toLowerCase();
  while (i--) {
    if (obj.indexOf(this[i].toLowerCase()) != -1)
    {
      return true;
    }
  }
  return false;
}

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 200 total points
ID: 34939310
replace line 13 by :
 if (obj.toLowerCase().indexOf(this[i].toLowerCase()) != (-1))

Open in new window

0
 
LVL 16

Assisted Solution

by:sjklein42
sjklein42 earned 200 total points
ID: 34939322
Fixed your parens

    if ((obj.toLowerCase()).indexOf(this[i]) != (-1))

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34939327
For my solution, you should also change the badStuff line to be all lower case.  Then you don't need to lower-case it every time it is used.

var badStuff = new Array("aaa","bbb","ccc","ddd","eee");

Open in new window

0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question