I have tried it but it does not let me pass


#1

Here are the details :
Your second "for" loop

Okay! Last loopy step: add another for loop, this time inside the body of your if statement (between the if's {}s).

This loop will make sure each character of your name gets pushed to the final array. The if statement says: "If we find the first letter of the name, start the second for
loop!" This loop says: "I'm going to add characters to the array until I
hit the length of the user's name." So if your name is 11 letters long,
your loop should add 11 characters to hits if it ever sees the first letter of myName in text.

And here is what I have tried :

/*jshint multistr:true */

var text = "This dish is good";
var myName = "Francis Lao";
var hits = [];

for(var i = 0; i < text.length; i++)
if(text[i] === 'F')
{
for(var j = 0; j < myName.length; j++) hits.push(myName[j]);
}
console.log(hits);

The code indeed works, but the it does not let me pass. And it says : "Oops, try again.
** Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.**"

I don't know what "Oops, try again" is talking about.

Did I do anything wrong?


#2

Your code work correct :slight_smile:
But the instructions want something different from you.

Maybe you will see what i mean if you execute this

var myName = "Francis Lao";
var hits = [];
for(var j = 0; j < myName.length; j++) hits.push(myName[j]);

Did you search in any text here ?

No you didnt, but you get the correct result.

Pleae rethink your code and if you have questions i am here :slight_smile:


#3

This also does not work. I don't get the output the website is expecting.

/*jshint multistr:true */

var text = "Fabulous";
var myName = "Francis Lao";
var hits = [];

for(var i = 0; i < text.length; i++)
if(text[i] === 'F')
{
for(var j = 0; j < myName.length; j++) hits.push(myName[j]);
}
console.log(hits);

Here is my console output :
[ 'F', 'r', 'a', 'n', 'c', 'i', 's', ' ', 'L', 'a', 'o' ]

Here is the hint from the website :
Your loop should stop when it hits the value of the first iterator (say, i) plus the length of your myName variable.

I can't think of anything else that make it works!


#4

Solved! I need to add "+ i" to the for-loop.
/*jshint multistr:true */

var text = "Fabulous";
var myName = "Francis Lao";
var hits = [];

for(var i = 0; i < text.length; i++)
if(text[i] === 'F')
{
for(var j = 0; j < myName.length + i; j++) hits.push(myName[j]);
}
console.log(hits);

But will this likely make the output wrong? "myName.length + i" will likely make the program access characters out of bounds.


#5

Your var text is to short:

var text = "The great Francis Lao";
var myName = "Francis Lao";

This should give you as result

[ 'F', 'r', 'a', 'n', 'c', 'i', 's', ' ', 'L', 'a', 'o' ]

#6

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