Solved

JavaScript: Case insensitive test against each item in array

Posted on 2011-02-20
4
789 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 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

707 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