Search Text for Your Name (FINE TUNED)


I was about to give up, but thanks to God I searched w3schools and found the slice() method!!!
NEVER NEVER GIVE UP. IT WILL TAKE A LOT OF TIME, but it is for the best.

/*jshint multistr:true */

var text = '01234 Roberto 5678  m1abc 9481 ROBERTO roberto', myName = 'roberto', hits = [], temphits=[];

var testMyName = function (text, myName) {
    var textLower = text.toLowerCase();//lowercase
    var textToArray = textLower.split("");//string to array
    myName = myName.toLowerCase();
    for (var i=0 ; i < textToArray.length; i++) {
        if (textToArray[i]===myName[0]) { //check if the first letter is the same of the first letter of the name
            for (var j = i; j <i+myName.length;j++){
                temphits.push(textToArray[j]); //put every letter that is found after the first letter to a temp Array.
                if (textToArray[j]===myName[myName.length-1]) { //when it reaches a letter that is equal to the last letter of the name, it will put it in the final array.
                    hits.push(temphits.slice(-myName.length));// throw to the array "hits" when the name is found
console.log('My name appear: ', hits.length, 'time(s)');


testMyName(text, myName);


I'm not really sure if it works though. When I run it, it says that "My name appear: 10 time(s)", but within the text, the name roberto only appears thrice.

I'm guessing eventually it is needed to make the name (now dissected into arrays) into a text again for it to work?

This is 2complex4mi.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.