Search Text For Your Name


#1

Search Text For Your Name

Only the "P" comes out. "addy" doesn't. Any thoughts?


/*jshint multistr:true */
var text = "Productive hungry Paddy is trying to code";
var myName = "Paddy";
var hits = [];
for(i = 0; i < text.length; i ++ ){
    if (text[i] === "P") {
        for(j = i; j < i + myName.length ;j++)
        hits.push(text[i])
}
}
if (hits.length === 0){
    console.log("Your name wasn't found!");
}
else{
 console.log(hits);   
}


#2

Change (text[i]) to (text[j]). This 2nd loop executes if the 1st loop finds an uppercase P. J is assigned the P and push collects the characters in the text string following P into the hits array. The 2nd loop runs until the number of characters in the text string is equal to the length of the myName variable.


#3

"Paddy" comes out. but so does "prod". It shouldn't tho because "prod" is not part of the myName var?


#4

@paddyalvr

"Produ" is also coming along with your name because as we know if statement will only the first letter of your name i.e 'P' once it found that it will start pushing the next character of that letter and will push element of that letter to the length of your name ...
your name "Paddy " contain 5 element so does "produ"
once it printed out "produ "of 5 element it searches for next word begin with 'p'......

Hope you will understand my explanation :slight_smile:


#5

Noted. Thank you both for the replies :slight_smile: Any chance you would know how to fix the code so only "Paddy" comes out? Thanks :smiley:


#6

@paddyalvr

/*jshint multistr:true */
var text = "Productive hungry Paddy is trying to code providig some Padding to html body";
var myName = "Paddy";
var hits = [];
for(i = 0; i < text.length; i ++ ){
if (text[i] === "P") {
if(text[i+1] === "a"){
if(text[i+2] === "d"){
if(text[i+3] === "d"){
if(text[i+4] === "y"){

    for(j = i; j < i + myName.length ;j++)
    hits.push(text[j]);
    }
       }
            }
                     }

}
}
if (hits.length === 0){
console.log("Your name wasn't found!");
}
else{
console.log(hits);
}

yes there are ways to print out only "Paddy" :smile:

1....you have to be more specific before pushing character to hits array for that you have to add those 4 lines of extra code of if condition which you can see from code above but it might look not a good style of coding that why I have another way to that, next way down

2...in this you only need to use a long condition inside your if parenthesis i.e

if(text[i] === "P" && text[i+1] === "a" && text[i+2] === "d" && text[i+3] === "d" && text[i+4] === "y" )

this line of code will console only the name which you want..
if you are not specific by using all those argument in if statement you will get "Paddi".

 check this out if it's working :slight_smile:

#7

Hi everyone,

when i write my name, utku, using lower case it prints: [ 'u', 't', 'k', 'u', 'u', ' ', 'a', 'm', 'u', 't', 'k', 'u', 'u', ' ', 's', 'o' ]

var text = "haha I utku am trying this for the first time, and I have to find\
my name utku somewhere back in time.";

var myName = "utku";
var hits = [];

for (var i = 0; i < text.length; i++) {
if ( text[i] === "u") {

    for (var j = i; j < (myName.length + i); j++) {
        hits.push(text[j]);
    }
}

}
if (hits.length === 0) {
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}

but when i change name to Utku, with the first letter upper case, now it is correct like: [ 'U', 't', 'k', 'u', 'U', 't', 'k', 'u' ]

why this occurs. (maybe using same letter twice?)


#8

When your myName variable is "utku", the 1st loop starts passing through the text string and executes when it finds the 1st u in the myName variable. The 2nd loop if condition is met and pushes all the characters after u into a hits array and displays "u","t","k","u" to the console.

Then, the 1st loop is triggered again by the last letter in utku which is u. The 2nd loop now collects the last u, the space between u and a, the a and the m. The hits array displays "u"," ", "a","m".

Now, your name appears again, hits collects "u","t","k","u" into the array. When the 2nd loop kicks in, the array collects u, the space between u and s, the s and the o. The hits array collected "u", " ", "s", "o".

Now, when you change the 1st u to a capital U in your myName variable the hits array will only collect the letters after a capital U as the lowercase u doesn't trigger the loops. This is why you only log "U","t","k","u".


#9

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