[HELP]- 5/7 (Your second "for" loop)


#1

I've written:

var text= 'My name is André. You are...'
var myName= 'André'
var hits= []

for (i=0; i>text.length; i++) {
    if (text[i]==='A') {
        for (j=i; j=(i+myName.length); j++) {
            hits.push(text[j]);
        }
    }
}

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.

Could someone say where I mistaked?


[HELP]- 12/13 (Loops and arrays II)
[HELP]- 12/13 (Loops and arrays II)
#2

it is in your second for loop:

(j=i; j=(i+myName.length); j++)

this should be:

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

you want j to increase till it the same as i + length of name, so you have to use a smaller then sign (<), not a equal sign, this will just j a new value


#3

Hello again ('@andrecdch'),

this is your code:

var text= 'My name is André. You are...'
var myName= 'André'
var hits= []
for (i=0; i>text.length; i++) {
    if (text[i]==='A') {
        for (j=i; j=(i+myName.length); j++) {
            hits.push(text[j]);
        }
    }

ok, after you declare a var, it's important to put a semi-colon

an example: var fruit = "apple" ; <--- (the semicolon)


after you add a semicolon to all of your you're variables, you should add var in the beginning of your for loop.

it should look like this:

 var i = 0


after you fixed that you should look at the code in the if statement inside your for loop
you need to put var in front of j = i;

it should look like this:

var j = i;

after that is complete, you should change:

j=(i+myName.length);

into this:

j < (myName.length + i);

finally in you first for loop change:

 i > text.length

into this:

 i < text.length

#4

:smile: Don't forget to add this:

console.log(hits);

#5

and now your code should look like this:

var text= 'My name is André. You are...'; 
var myName= 'André';
var hits= [];
for (var i=0; i<text.length; i++) {
    if (text[i]==='A') { 
        for ( j=i; j < (myName.length + i); j++) {
            hits.push(text[j]);
        }
    }
}
console.log(hits);

#7

Hey , in your code, if I add another string like Alex, that prints out too, which is something we dont want!!
My name is André. You are..??? Alex how are you ??

Output: [ 'A', 'n', 'd', 'r', 'é', 'A', 'l', 'e', 'x' ]


#8

Yea, given it start with a capitalized A as well, this be pushed to output as well. Any idea how you could resolve this issue? Below are some hints which might help you: (you need to fill in the ....)


We could also check the last letter of the name


if (text[i]==='A' && ..... == 'é')


if (text[i]==='A' && text[i+.....] == 'é')

let me know if you need more help


#9

(' @betapro05987 ' ) and (' @stetim94 ') , :grin:
Good day,

the lessons code:

var text= 'My name is André. You are...'; 
var myName= 'André';
var hits= [];
for (var i=0; i<text.length; i++) {
    if (text[i]==='A') { 
        for ( j=i; j < (myName.length + i); j++) {
            hits.push(text[j]);
        }
    }
}
console.log(hits);

This is excercise,

5/7 (Your second "for" loop)

This is the part were we add our second for loop, the lesson does not state that we should add another string called 'Alex'. ('@betapro05987') the reason why, it also prints out 'Alex', is because the if statement checks if a letter in the given string (which is stored in the variable text) starts with 'A' and then when that condition is met it pushes the rest of that word that starts with the 'A' character into the hits array.

You're probably on lesson,

7/7 (VICTORY!)

this is the lessons that states that you should you should fine-tune your program so that when it has multiple values that start with the same character, it would only print your name.

@betapro05987 you said that:

" Hey , in your code, if I add another string like Alex, that prints out too, which is something (we) dont want!! "

But this code was meant for lesson 5/7 and is correct for this lesson,

Your problem is part of lesson 7/7 were you have to fine-tune your code on your own and make your program better so that it can take more values and still return the one you want. etc.


Have a good day :raised_hand: :grinning: my friends and keep up the good work :smile:


#10

About the problem you addresed of founding 2 names who start with the same letter, i tried to make a script to address this problem but it seems to work only with my name, when i tried other names and changed the nmae string it didnt work. this is the code:

var text = "Hey my name is Ziv, and i live in Israel. My name is Ziv but my friends call me Zed, its just a little nickname they made. Ziv Ziv Ziv Ziv Ziv wahoo ya wha blah blah ziv Ziv";

var myName = "Ziv";

var hits = [];
var number = 0
for (var i = 0; i < text.length; i++){
if (text[i] === myName[number]){
for (var j=i;j < (i + myName.length); j++) {
if (text[j] === myName[number])
hits.push(text[j]);
number = number + 1
}
}
}
if(hits <1){
console.log("Your name wasn't found!");
} else {
console.log(hits);
}

if you could help me to figure out why the code isnt flexibale with all names i would highly appreciate it!


#11

refering to my last replay it seems to stop after the first Z it finds, so if its for ex hey Ziv Ziv hi Ziv it will stop searching after the first Ziv and i cant find the answer for it! im guessing its something related to the for loops


#12

A post was split to a new topic: Need help with 5/7 second for loop


#13

@blacklight7,

If you want to make a program that will overcome storing multiple values and instead store your chosen value you can:

use the lessons code provided on comment 8 (you can customize the text and myName part to your own chosen values)

then you should focus on changing the if statement inside the for loop so that it would only print your name, you can see comment 7 and use the clues stetim94 provided



#14

If you need more help beside the hints i already provided, let me know