Search Text for Your Name in JS

I Listening In
i. What you’ll be building 1/7
Save and Submit
ii. Declare your variables 2/7

var text = "Hello! I am Sunakshi. Sunakshi  is an Indian name. Sunakshi is not a very common name."
var myName = "Sunakshi";
var hits = [];

iii. Your first “for” loop 3/7

var text = "Hello! I am Sunakshi. Sunakshi  is an Indian name. Sunakshi is not a very common name."
var myName = "Sunakshi";
var hits = [];
for ( var i = 0; i < text.length; i++) {
}

iv. Your “if” statement 4/7

var text = "Hello! I am Sunakshi. Sunakshi  is an Indian name. Sunakshi is not a very common name."
var myName = "Sunakshi";
var hits = [];
for ( var i = 0; i < text.length; i++) {
    if ( "S" === text[i]) {
    }
}

v. Your second “for” loop 5/7

var text = "Hello I am Sunakshi. \
Sunakshi  is an Indian name. Sunakshi is not a very common name.";
var myName = "Sunakshi";
var hits = [];
for(var i = 0; i< text.length; i++) {
if (text[i] === "S") {
for(var j = i; j < (myName.length + i); j++) {
hits.push(text[j]);
}
}
}            

vi. Log it! 6/7

var text = "Hello I am Sunakshi. \
Sunakshi is an Indian name. Sunakshi is not a very common name.";
var myName = "Sunakshi";
var hits = [];
for(var i = 0; i< text.length; i++) {
if (text[i] === "S") {
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);
}

vii. Victory! 7/7
Save and Submit!

Advertisements

4 thoughts on “Search Text for Your Name in JS

  1. So, is there a way to tune the code so it only accepts the names that you choose?
    For instance, if we had Sam and Shueffue in the text it will show both names, I would like to know

    Like

  2. I compared your code to mine for 5/7, and it still won’t go through for me. It says “Oops, try again. It looks like your second ‘for’ loop isn’t pushing values to the hits array. Make sure it’s working properly and that myName’s text appears somewhere in the text variable.”
    Please help, I don’t know what I am doing wrong. Here is my code:

    var text = “Jess likes food Jess likes food”;
    var myName = “Jess”;
    var hits = [];
    for (var i = 0; i < text.length + 1; i++);{
    if (text[i] === "J") {
    for (var j = i; j < (myName.length + i); j++); {
    hits.push(text[j]);
    }
    }
    }

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s