Does this code meant to compare only first letter?


#1

https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/6?curriculum_id=506324b3a7dffd00020bf661

Code works , but prints the word "see" too. its just comparing the first letter of each word.

Can you Explain ?


/*jshint multistr:true */
var text= ("Hello How are you sid ? nice meeting you sid , see you sid");
var myName="sid";
var hits=[];
for(var i=0;i<text.length;i++)
{
    if (text[i] === "s")
    {
  
        for(var j=i;j<i+myName.length;j++)
        {
            hits.push(text[j]);
            hits[0];
        }
   
}
}
if (hits.length===0)
{
    console.log("Your name wasn't found");
}
else
{
    console.log(hits);
}


#2

yea, its only checking first letter, its not a perfect program yet

This problem can be (partly) be overcome by spelling Sid with uppercase S. Its quit common to spell names with uppercase first letter


#3

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