Solved

JavaScript: Case insensitive test against each item in array

Posted on 2011-02-20
4
739 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
  • 2
4 Comments
 
LVL 42

Accepted Solution

by:
David S. earned 400 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 50 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 50 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now