Is something wrong with the code?


#1


why  is {E, b, l, a}   in the output??


#2

this program has a flaw, it finds all words starting with a uppercase E, if you don't want this you would have to improve the program


#3

Thank u so much... will try to improve the code


#4

you could use substring(), or also check for the last letter text[i + myName.length -1 ] with another if statemetn


#5

Okay.....Well thanks again


#6

i tried something like this though

var text="hi i'm ritzy!! wat is ur name?? yo!! i'm Merrick... nyc to meet u";
var hits=[];

for(var i=0; i<text.length;i++)
{
    if(text[i]==='r'&&text[i+1]==='i'&&text[i+2]==='t'&&text[i+3]==='z'&&text[i+4]==='y')
    { 
        
        for(var j=i;j<i+myName.length;j++)
        {
            hits.push(text[j]);
        }
    }
}
if(hits===0)
{
    console.log("Your name wasn't found!");
}
else
{
    console.log("not found");
}

#7

where did the declaration of myName:

myName='ritzy';

go? and if your name is found, you should log it:

else
{
    console.log(hits);
}

#8

well i just removed it...
but u r right hw did it work??
hw did it calculate myName.length??

if I log console.log(hits);
I get [] as output... didn't find it right... cuz i wanted it to display "not found" wen my name is not found


#9

if you need help with the code, post an updated version code.

If you name isn't found the if clause run:

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

if your name is found the else clause will run:

else
{
    console.log("not found");
}

it is so weird to print not found while your name is found..


#10

i m so sorry... got ur point


#11

how abt it now?


#12

that looks good :slight_smile: :slight_smile:


#13

thanks a lot for ur help!!! :slight_smile: