Help 5/7


#1


var text=("Hello Master Adywan")
var myName=("Hexy")
var hits=[""]
for (var i = 0; i < text.length ; i++){
    if (text[i] === 'E') {
      
    }
};

I need help how do I complete this exercise ? Link https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661#


Search Text - "Second 'For' Loop"
#2

you have a few syntax errors as well as incompleteness.
I can help you walk through this if you'd like.
first, get rid of the parenthesis around

and

, strings don't need parenthesis AND quotation marks, just quotation marks.
also an easy thing,

take the quotation marks out of the brackets
in my next post I'll walk through the for loop for you


#3

You start the loop correctly with

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

because that's exactly what you're looking for.
Now, inside of the loop, the if statement is also correct. So far you have

for (var i = 0; i < text.length ; i++){
    if (text[i] === 'E') {
      
    }
};

which is perfect.
Now, to the explaining part on what to do.
You're only missing a small part which is another for loop.
This next for loop needs to introduce a new variable, and the instructions say that it should start like this:

for(var j = i; // rest of loop setup

Now, to set up the loop, we know that j needs to start at i, it needs to stop at myName + i, and it needs to increment by one.
this would look like:

for(var j = i; j < myName + i; j++) {

Inside of the for loop you need one line, and it's using the push method.
The instructions show you how to use the push method.
Basically, the syntax is this:

(what you are going to push into, such as hits).push(what you are pushing);

your line would look like:

hits.push(text[j];)

So I hope this makes sense. I did just walk you through it and supply you with the correct code, but I wanted to try to explain it as well, because I'm sure you've been trying to figure it out.
hope I helped :)))


#4

Woah Thank you so much ! Your a great help! Helped me a lot


#5

1 question what does this .push do and why is it used ?


#6

From the lesson:
Finally, in the body of your loop, have your program use the .push() method of hits. Just like strings and arrays have a .length method, arrays have a .push() method that adds the thing between parentheses to the end of the array.

This is the only documentation I could find:

The push method just add things to an array, and there is more to it, but I'm not very versed in JavaScript, so my understanding is extremely minimal.


#7

Thanks! Have a Good Day


#8

Only one correction. .length is a property not method. But your explanation is great(!!!) and can help anyone. :+1:


#9

You're right! Thank you for mentioning this! Thanks for the support too, :slight_smile:


#10

Yep ! Really helped ! You guys are guys too :ok_hand:


#11

So helpful -- thank you for your thoroughness!!


#12

Hello! I am trying to understand final code (specially second loop during all day :slight_smile: And I did it :wink:
May be my explanation will be helpful for someone.
I made little different text for better understanding.
as we know that in ARRAY count starts from position 0 (zero) My text is:

var text = '0123456789ALEX';

0123456789 (the same positions as in array) and after the name "ALEX" we are looking for: A (position 10), L (position 11) E (position 12) X (position 13).

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

This code select all symbols in 'text' one by one.

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

Here it is if statement. When loop found the symbol A code in {} after runs

{   
	if (text[i] === "A") 
{

in {} there is second loop

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

At first I could not understand why we plus ' i ' to myName.length
So let's go step by step:

When "if" statement found symbol "A' the loop on {} run

 i = j // new variable ' j ' equal  ' i '

In name ALEX symbol A - at 10 position.

1. Loop runs.

j=i=10; j < myName.length (in myName  4 symbols) + i=10; j++

= the same code: RESULT: A

j=i=10; j < 4+10; (10 < 14) it is true then _hits.push(text[j])_ run and loop run _j++_

2. next symbol is L position 11. j added 1 position using (j++) So the loop:
j=i=10; 10+1 < 4+10 (11<14) it is true - then hits.push(text[j]) j=11 and L at 11 position. **So in 'hits' array is L**. j++ run
**3. next symbol is E**. position 12. _j_ added 1 position (j++) **So the loop:**
j=i=10; 11+1 < 4+10 (12 < 14) it is true - then hits.push(text[j]) j=12 and E at 12 position. So in 'hits' array is E. j++ run
4. next symbol is X. position 13 and j added 1 position (j++) so th loop:
` j-i=10; 12+1 < 4+10 (13<14) it is true then hits.push(text[j]) j=13 and X at 13 position. So in 'hits' array is X. j++ run
5. Now we have in 'hits' array 'A L E X'.
6. loops stops because j=i=10; 13+1 < 4+10 (14<14) it is false, Loop stopped and we've got final result.

Rest of code I did not insert here as it is clear.
Something like this. Sorry for any mistakes. I jus was tried to show my thoughts...


#13

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