jonjon
jonjon

Reputation: 121

counting words within a text document

I've written some code to count the number of times a word appears within a text. Before adding the function findDuplicates, the code works. For example, it tells me that the word hello appears 3 times within my text. I added the function findDuplicates so that it would tell me whenever only after I've pushed the button for it to calculate the count. Currently, when I push the button, nothing happens. I checked the console and I'm not getting errors. I'm wrecking my brain trying to figure what mistake i made with the function.

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>WordBubble</title>
    <link rel="stylesheet" type="text/css" href="wordbubble.css">
    <script type="text/javascript" src="jquery-1.10.2.js"></script>
</head>



<body>



<script type="text/javascript">


// ajax call to get comments document
function findDuplicates (myWord) {
$.get( "comm.txt", function( text ) {


    words = text.split(' '),
    sortedWords = words.slice(0).sort(),
    duplicateWords = []
    var myWord = "hello";


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






$(document).ready(function(){
  $("button").click(function(){
   findDuplicates();
  });
});
}
</script>

 <button>Button label</button> 




</body>
</html>

Upvotes: 0

Views: 42

Answers (1)

Faris
Faris

Reputation: 917

If I'm not mistaken, your $(document).ready code is inside the findDuplicates function. That means the button isn't actually calling findDuplicates since the $("button").click code never ran. Try moving the $(document).ready part outside the function scope.

Upvotes: 1

Related Questions