?
Solved

Highlight duplicates in lists over three characters in length

Posted on 2013-01-25
2
Medium Priority
?
320 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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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

839 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