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
Solved

JavaScript: Case insensitive test against each item in array

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

790 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