Becky
Becky

Reputation: 2289

For loop condition issues

I am having troubles figuring out the for condition for a javascript learning course I am doing.

I am not getting normal errors because I am doing it in the course, but the error I am receiving in the course is this...

Oops, try again. Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.

These are the instructions:

First, you'll want to set your second loop's iterator to start at the first one, so it picks up where that one left off. If your first loop starts with

for(var i = 0; // rest of loop setup
your second should be something like

for(var j = i; // rest of loop setup
Second, think hard about when your loop should stop. Check the Hint if you get stuck!

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. For example,

newArray = [];
newArray.push('hello');
newArray[0];   // equals 'hello'

This is my code

var text = "Hello, my name is Becky. What is your name?\
I repeat, my name is Becky. Can't you figure out that my\
name is Becky. Becky!!!!";
var myName = "Becky";
var hits = [];
for (i = 0; i < text.length; i++) {
    if (text[i] === 'B') {
        for (var j = i; i < myName.length; i++) {
            hits.push();
        }
    }
}

I know the issue resides in this line:

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

I just can't figure out exactly how I need to structure it.

UPDATE:

Final answer of question:

/*jshint multistr:true */
var text = "Hello, my name is Becky. What is your name?\
I repeat, my name is Becky. Can't you figure out that my\
name is Becky. Becky!!!!";
var myName = "Becky";
var hits = [];
for (i = 0; i < text.length; i++) {
    if (text[i] === 'B') {
        for (var j = i; j < (i + myName.length); j++) {
            hits.push(myName);
        }
    }
}
if (hits === 0) {
    console.log("Your name wasn't found!");
} else {
    console.log(hits);
}

Upvotes: 0

Views: 105

Answers (2)

Nelson Teixeira
Nelson Teixeira

Reputation: 6562

I'll give you the solution because you can learn more from the direct solution than from banging your head on that one.

var text = "Hello, my name is Becky. What is your name?\
I repeat, my name is Becky. Can't you figure out that my\
name is Becky. Becky!!!!";
var myName = "Becky";
var hits = [];
for (i = 0; i < text.length; i++) {
    if (text[i] == 'B') {
        var equal = true;
        for (var j = 0; j < myName.length; j++) {
            if (text[i + j] != myName[j]) {
                equal = false;
                break;
            } 
        }
        if(equal) hits.push(myName);
    }
}

There may be other ways to reach this result. This is one of them.

Explaing what "push" does:

Arrays are lists of variables. You store a value in a variable like this:

var myNumber = 777;
var myName = "Nelson";

An array declaration looks like this:

var myNumbers = [];

then you put something inside of it, like this:

myNumbers.push(333);
myNumbers.push(555);
myNumbers.push(777);

then if you try: console.log(myNumbers), it will print: [333, 555, 777]

if you add another push:

myNumbers.push(999);

will add 999 to the list resulting in [333, 555, 777, 999]

Check this demo

got it ? take a look here to more detailed explanation:

http://www.hunlock.com/blogs/Mastering_Javascript_Arrays

Upvotes: 1

phpsmashcode
phpsmashcode

Reputation: 746

Am not sure what you are trying to achieve,

Here is something may help

var text = "Hello, my name is Becky. What is your name?\
I repeat, my name is Becky. Can't you figure out that my\
name is Becky. Becky!!!!";
var myName = "Becky";
var hits = [];
for (i = 0; i < text.length; i++) {
    if (text[i] == 'B') {
        var res = '';
        for (var j = i; j < i+myName.length; j++) {
            res = res+text[j];
        }
        if(res == myName)
        {
            hits.push(myName);
        }
    }
}
console.log(hits);

Here is demo

Upvotes: 1

Related Questions