# Tag Archives: js I’ve mentioned many time on this blog how I am a big fan of codecademy. I think this is an excellent way to develop some coding skills in a simple hand-held way. I’ve been following the javascript track for some time and really enjoy it. Recently, Ruby and Python tracks have been introduced, although I have only given each one minimal attention so far. In the future, I am hoping to concentrate on the web programming elements in order to provide some context for the javascript work.

Presently, I have been stuck on a problem that should be easy, but the walkthrough does not seem to guide you in the right direction. The problem is intended to take a string of text and search for your name within this text. I think the difficulty arises from the fact that the walkthrough sounds like you should be solving the whole problem, but may actually be asking for less.

Below, with a few extra details, I present a solution that effectively gets the job done. I’ve included some documentation so you can see what I was thinking as well as some debugging aids to visualize what the program is doing at each step. This solution will get a ‘pass’ for the problem, but I am wondering how this might be simplified.

/*jshint multistr:true */

var text = “hdajkslhgjalhjghjklhjackfghjdskghjkdfajlk”;  //the text we search through
var myName = “jack”;                                                  //what we are looking for
var hits = [];                                                                  //array storing match
var j=0;                                                                         //a counter for myName array

/////////first loop for match with myName////////////
for (i=0;i< text.length;i++){
console.log(i); // enumerates position in text array
if (text[i] === myName[j]){

//////advances position in myName array, second loop continues search/////
for(var j=i;j<i +myName.length;j++){
console.log(“testing if”,text[j],” = “,myName[j-i]); //shows match test
if(text[j] != myName[j-i]){
hits.length = 0; //if no match, hits array is cleared
j=0; //array position in myName is reset
break; //exits inner loop
} //end ‘if no match’

if (text[j] === myName[j-i]){ //for each match, letter is pushed
hits.push(text[j]); //onto hits array
} //end ‘if match’
} //end inner loop

/////if reach size of myName array ////////
if (hits.length === myName.length){
console.log(“we found your name: “, hits); //output match
break;
} //end ‘size of array
}//end outer loop
}//end program

/* output
0
1
2
3
testing if j = j
testing if k = a
4
5
6
7
8
9
testing if j = j
testing if a = a
testing if l = c
10
11
12
13
testing if j = j
testing if g = a
14
15
16
testing if j = j
testing if k = a
17
18
19
20
testing if j = j
testing if a = a
testing if c = c
testing if k = k
we found your name: [ ‘j’, ‘a’, ‘c’, ‘k’ ]

*/
{"ft_lang_label":"__label__en","ft_lang_prob":0.8380899,"math_prob":0.7305103,"size":2750,"snap":"2021-31-2021-39","text_gpt3_token_len":742,"char_repetition_ratio":0.12855062,"word_repetition_ratio":0.06378601,"special_character_ratio":0.292,"punctuation_ratio":0.10834814,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9594793,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,6,null,6,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-27T06:00:51Z\",\"WARC-Record-ID\":\"<urn:uuid:89444828-1f2b-4d06-b69d-398f7400d12f>\",\"Content-Length\":\"49285\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7e7f8ed1-ac9c-4516-860c-6a3510f91715>\",\"WARC-Concurrent-To\":\"<urn:uuid:aa4d8d3d-8685-4b76-9293-6361ac40908a>\",\"WARC-IP-Address\":\"192.0.78.24\",\"WARC-Target-URI\":\"https://downhouse.software/tag/js/\",\"WARC-Payload-Digest\":\"sha1:4JLNIFXXE6LOIBELEGEP3DRKR2F2PPQV\",\"WARC-Block-Digest\":\"sha1:IDNNANKOV6P5BCYFLIYQIXR44EF6A2PA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046152236.64_warc_CC-MAIN-20210727041254-20210727071254-00265.warc.gz\"}"}