No errors just confused on what to do!


#1

Super Lost don't know what to do.

Exercise 5 Search Text for your Name.

Just don't know what to do.


/*jshint multistr:true */
var text = "Hi, my name is Shane. Shane is awesome!"
var myName = "Shane"
var hits = []

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


#2

Hey.

No worries lets go through this step by step...

First for loop

So your first for loop is responsible for finding the letter you specified in your if statement. It will loop through each index of the text string until there are no more text characters left. That's what text.length is for. Once i reaches the length of the text string then there's no more need to loop because we've reached the end.

Onto the if statement. When the if statement is true - when we find an "S" - we execute the if block. What goes in the if block? Another for loop.

Second for loop

This second for loop's job is to now extract your name (since we found the first letter corresponding to your name), and push each character to a new array called hits[].


The second for loop's parentheses should look like this:

for ( var j = i; j < i + myName.length; j++ )

But what does this do?

var j = i;

First we initialise j.

We then assign j the value of i. Now remember, i is equal to the index that we're currently at. And since we found our first "S" at index [15] lets use that as the scenario.


j < i + (myName.length)

Next we set up our condition to be tested each iteration of the second for loop. This is the brainy part. We want this for loop to fire as long as the index we're current on [15] is less than the index we found the first letter [15] + the length of the name we want to extract. Therefore this loop will stop when j === 20. Because the length of Shane is 5. (15+5 = 20). This is how we know when to stop this for loop.


j++

Lastly the final-expression. This simply iterates our j variable after we complete the for loop. So we test the condition > do the code in the for loop > then we iterate the variable.

What goes in our for loop statement to execute?

hits.push(text[j]);

We're simply pushing the j index on the text[] string to a new array called hits[]; as we discussed before.

So in full the whole program should like like

/*jshint multistr:true */
var text = "Hi, my name is Shane. Shane is awesome!"
var myName = "Shane"
var hits = []

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

console.log(hits);

Hope that makes sense. If not let me know :slight_smile:


5. Second "for" Loop
5. Please help
How do I end the second For Loop?
Stuck here not sure whats the problem?
Search Text for Your Name: Your second "for" loop
Lesson 5: Your second for loop. Help
Trying to make sense of "your second for loop" equation
#3

Wow, man thanks.
I am confused one thing where did you get the [15] from also is their any way I can friend you, I am new to this so I might need your help in the future.
P.S. That whole thing must've taken a long time, thanks for the effort put into that.


#4

You're welcome.

If you count how many characters (including spaces and special characters) are in the string "Hi, my name is Shane. Shane is awesome!". You'll find that 15 is the index value of the first "S". Don't forget to start counting from 0.

And I don't believe you can be-friend but you're more than welcome to PM me with anything else you may get stuck on.


#5

Ok, thanks man.
Btw do you know more than just Javascript.
Just a random question how do you PM someone.


#6

Yeah I'm familiar with a few others. Go to my profile and you should see a Message option there.


#7

Hey there,

I'm hoping that you'll spot my error. Each time I submit the following text - which looks fine to me - I keep getting this error: "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."

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

for (var i = 0; i >= text.length; i++) {
if (text[i] === 'S') {
for (var s = i; s < i + myName.length; s++) {
hits.push(text[s]);
}
}
}

console.log(hits);

Of course, I also have a variable that contains the text for this exercise. I just didn't want to clutter up this post. Cheers.


#8

Hi.

The reason it's not pushing out anything to the hits[] array is because in your first for loop you're testing whether i is greater than the current position in the text string. This for loop won't execute because i (0) is most likely not (in this case) greater than the amount of characters in your text variable.

Try this instead.

for (var i = 0; i < text.length; i++) {

Now this for loop will run as long as i is less than the amount of available characters in your text string.


Also, you're more than welcome to post all of the code, no matter how clutter-y it may seem. Every line of it will make it easier for us to debug.

Thanks


#9

Duh! This is one of those times when you end up not being able to see the wood for the trees.

Thanks for pointing out my error. I had actually made that change earlier but then accidently controlZeded back to an earlier version. It's the same as editing an essay, good to get someone else to take a look as you end up reading what you think is there not what is actually there.

Thanks again for the help!


#10

That was an amazing explanation of what the second for loop does. It made me understand it completely. I was looking for a post like this that made me better understand what's going on here.

Thanks for the detailed information!


#11

Thanks, this really helped me understand the second "for" loop. However, I was wondering how you would know to have an empty hit array in the first place. A lot of the course is just mimicking what we see in examples without a lot of explanation of how to get there in the first place. That is the part of Javascript that is a deep mystery to me and I'm really trying to learn the right questions to ask myself when I'm getting ready to program something in Javascript so that I'll be able to do it on my own after the course.


#12

In this example it wouldn't actually matter whether the hit array was empty because .push will push values to the array regardless.

But this is the part in programming where we have to look back on the knowledge that we have acquired and try to come up with a solution. What you're asking basically comes down to logic at this point - and you've covered all of what I'm about to tell you.

My question to you is: What do we use to check if something is true or false? An if statement, right? So that's part one to your question there.

The second part is how do we know whether hits[] is empty before initiating the process above. Well... we used text.length to check the length of a string. And conveniently enough array's and strings have similar functions, meaning that we can also check the length of hits[] by doing hits.length. This will return an integer pertaining to how many elements are in the array.

Now we want to combine those two thing together. It's as simple as going...

if (hits.length < 1 ) {
 // This block will run if there is less than 1 element in the hits array
}
else {
 // Other wise, do this. But else is optional
}

I encourage you to try this for yourself and see what kind of results you come up with by using this logic.

Try this site --> jsfiddle

It will allow you to play around with HTML, CSS and Javascript. But you don't need to fill out the HTML and CSS boxes. Just press f12 to bring up the developers window and select the console tab so that when you run the jsfiddle on the top left, you'll be able to see what the console is doing.


#13

angus you did an awesome job explaining this all bud so thank you for that. Now i just have one question. What if there was another word in the text that had the same first letter as your name...wouldn't that mess up the whole thing ? if not can you please explain why it wouldn't tthanks agani


#14

Yes you're right - it would. There's definitely ways of ensuring this doesn't happen though. But for now this example is just there to give you an introductory lesson on the true capabilities of the for loop.

If you're wondering what would happen if we had for example.. "Shoe" somewhere in the text string.

"Hi, my Shoe is Shane. Shane is awesome!"

All of shoe's characters plus the space after it would get pushed to the hits[] array. This is because as we know, we're printing values equal to the length of myName. i.e S(1) H(2) O(3) E(4) " "(5).


Slightly Advance Question about Search Text
#15

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