Solved

Highlight duplicates in lists over three characters in length

Posted on 2013-01-25
2
278 Views
Last Modified: 2013-01-25
My html page highlights duplicates just fine but I need JavaScript to highlight only those words with 4 or more characters. I don't at all understand JavaScript, so best to show me where to change from 4 occurrences to whatever I need. This code currently displays ALL duplicates regardless of the length of the word, making pages with lots of text extremely difficult to read when highlighted. The code will be used on our closed network only and using MSIE or Firefox.

If possible, I'd prefer to have each duplicate occurrence counted and display the count with the duplicate, ordered with the most duplicates listed first. But if that's too difficult, I'll settle for the first part of this question.


<!DOCTYPE html>
<html>
<head>
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.5.js'></script>
  
  <style type='text/css'>
    span.duplicate { background: #ffdddd;
		}
  </style>

<script type='text/javascript'>//<![CDATA[ 
$(function(){
var text = $('p').text(),
    words = text.split(' '),
    sortedWords = words.slice(0).sort(),
    duplicateWords = [],
    sentences = text.split('.'),
    sortedSentences = sentences.slice(0).sort(),
    duplicateSentences = [];


for (var i=0; i<sortedWords.length-1; i++) {
    if (sortedWords[i+1] == sortedWords[i]) {
        duplicateWords.push(sortedWords[i]);
    }
}
duplicateWords = $.unique(duplicateWords);

$('a.words').click(function(){
    var highlighted = $.map(words, function(word){
        if ($.inArray(word, duplicateWords) > -1)
            return '<span class="duplicate">' + word + '</span>';
        else return word;
    });
    $('p').html(highlighted.join(' '));
    return false;
});



});//]]>  

</script>
</head>
<body>

  <p>Bob is attempting to find the reason Janet is attempting to understand Bob and to reason with him.</p>
<hr />
<a class="words" href="#">Find duplicate words</a>
  
</body>
</html>

Open in new window

0
Comment
Question by:Qsorb
2 Comments
 
LVL 3

Accepted Solution

by:
Khilu earned 500 total points
ID: 38818169
Update the following condition (line no 23 in given code)


sortedWords[a].length > 3 && sortedWords[a+1].length > 3




for (var a=0; a<sortedWords.length-1;a++) {
if ( sortedWords[a].length > 3 
    && sortedWords[a+1].length > 3 
    && sortedWords[a+1] == sortedWords[a])
{
    duplicateWords.push(sortedWords[a]);
}
}

Open in new window

0
 

Author Closing Comment

by:Qsorb
ID: 38820717
Yes, takes care of the count adjustment to be highlighted. Thanks!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

757 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

21 Experts available now in Live!

Get 1:1 Help Now