0

How do I search through each word in a string of a

that matches an array?

Here is my prototype:

        $("#latte").on("click", function() {

            $('#lattePool').load('dictionary.txt'); 

            var text = $("#lattePool").text().toUpperCase();
            var words = text.split("\n");
            var dictionary = new Array();
            for(var i=0; i < words.length; i++) {
                dictionary[i] = words[i];
            };

            $("#notes p").each(function(){
                var nameFound = $.inArray($(this).text().trim().toUpperCase(), dictionary);
                if (nameFound === -1){
                } else {
                    alert($(this).text() + " found in array");
               }
            });

        });

When user click on #latte button, it loads a dictionary.txt text into a temporary pool call #lattePool, then it breaks down into individual words and then into a very long array.

Then, wehn user paste something in a contenteditable div #notes, it needs to find each words in the

tag with all the words in the dictionary array.

currently it only works with a single word in a

, not a long string.

Any ideas to make it check through each word of a

from an array?

Many thanks~!!!

1 Answers1

0

Instead of $("#notes p").each you need to get p tag text and split with space and loop through it to find out all words.

Try this:

$("#latte").on("click", function() {
        $('#lattePool').load('dictionary.txt', function(){
            var text = $("#lattePool").text().toUpperCase();
            var words = text.split("\n");
            var dictionary = new Array();
            for(var i=0; i < words.length; i++) {
                dictionary[i] = words[i];
            };
            var contentEditableTxt = $("#notes p").text();
            var splitCntEditableTxt = contentEditableTxt.split(" ");
            $.each(splitCntEditableTxt,function(key,val){   
                var nameFound = $.inArray(val.trim().toUpperCase(), dictionary);
                if (nameFound === -1){
                } else {
                    alert(val + " found in array");
               }
            });
        }); 
    });
Kiran
  • 20,167
  • 11
  • 67
  • 99
  • Thank you so much for the prompt reply. I modified the prototype to what Mikey and you suggested to synchronous, and with new split var, but on the $.each(splitCntEditableTxt), it didn't work. I tried adding an alert and it didn't fire a fail match and it seems like it didn't run the comparison code – Jesse Lim Jia Nian Jul 26 '17 at 06:38
  • I got 2 syntax error: 1, SyntaxError: expected expression, got '<' and 2, XML Parsing Error: syntax error Location: file:///C:/Users/.../dictionary.txt Line Number 1, Column 1: – Jesse Lim Jia Nian Jul 26 '17 at 06:58
  • Looks like issue with loading txt file. If you can solve that above code snippet will work. – Kiran Jul 26 '17 at 07:10
  • I tried to use alert(dictionary[123]); to test whether the array is created successfully and it did return a word. And if I use the old method $.inArray($(this).text().trim().toUpperCase(), dictionary); it did work but only a single word in each

    tag. Your code is on the right path and really solve the splitting the #note p part, but I don't know what I did wrong.

    – Jesse Lim Jia Nian Jul 26 '17 at 07:17
  • OMG, it works~!!! Thank you so much~!!! So Amazing~~~ Again, thank you so much for the prompt reply~~~ – Jesse Lim Jia Nian Jul 26 '17 at 07:50