How does this not find my name?


#1


var text = "fjkl Kasterajsd.df. adf.adfk
ajd.fadf;ajfaef.aefjvmadma. efKasterpme
pamfKaster djfakjd Kaster lka sndfkapejao
epakKastere a nvenvpaeovvbinomp,p;l,km
jnghbvekifkmlfLKasterasterkmjkj ;sna[pipe
Kastersooaefj[[m ";
var myName = "Kaster";
var hits = [];

for (i=0; i<text.length; i++){
    if (text[i] === "K"){
        for (j=i; j<(myName.length+i); j++){
            hits.push(text[j]);
        }
    }
}
if (text[i] !== "K"){
    console.log( "Your name wasn't found!" );
}
else{
    console.log(hits);
}.


#2

We do not see the first line provided in the code that enables continuation, and we do not see the continuation operator at line breaks in the text.


#3

When I ran the code without them it worked the same way with them, but if they actually do something please correct me if I am mistaken please. I assumed that they were unnecessary in carrying out the function of the code, but were there to make the code look uniform and easier to interpret,


#4

A valid string cannot contain line breaks without an escape. That's what the continuation operator does.

var text = " ... \
     ... "

#5

I found the problem it was on my second if statement

the condition should have been

if (hits.length === 0)

I assumed initially that the loop was going from character from character until it reached the character K, and it was initially before the if statement actually starts, but after it the loop changes. The loop was actually starting on the character K and adding the letters that follow it if they are the exact same group of character as the ones in the variable myName, so in this case Kaster, and then stores them into the hits variable. So in actuality the loop changes the hits variable not text, so when I created the if statement outside of the loop I should have made the conditions correlate with the hits variable not text.

Thank you for responding really quickly though I very much appreciated it if you didn't I probably wouldn't have revisited this and figured this out.


#6

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