0

I am new to JavaScript and is going through the tutorials here on www.codeacademy.com. I am trying to push a string into a new array however i am getting an error when i run the code saying

"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 myWord text appears somewhere in the text variable."

I have looked over the code but have no clue as to where the fault is?

var someText = "This is some text and i am trying to push this text into a new string containing the string text.";

var myWord = "text";

var hits = [];

for (var i = 0; i < someText.length; i++) {
    if (someText[i] === myWord[0]) {
        for (var j = i; j < someText[i] + myWord.length; j++) {
            hits = [];
            hits.push("text");
            hits[0]; //equals 'text'

        }
    }
}

any ideas how to get this to work?

11
  • Whats going on right here: someText[i]+myWord.length; Commented Oct 4, 2013 at 14:40
  • someText[i]+myWord.length is the stopping condition for the second loop. perhaps i didn't need to reference the index :S Commented Oct 4, 2013 at 14:43
  • Well, whats are you trying to check in that condition? Commented Oct 4, 2013 at 14:45
  • I am trying to check 'someText' for the first letter in 'myWord' then push (add) the number of characters equal to 'myWord's' length to an array. Commented Oct 4, 2013 at 14:49
  • Just saying, this condition j < someText[i] + myWord.length is going to evaluate to something like j < t20 Commented Oct 4, 2013 at 14:50

2 Answers 2

2

You reset hits at each iteration.

Remove hits = [] in your loop.

Sign up to request clarification or add additional context in comments.

Comments

0

Your second loop iteration is doing an illegal comparison j < someText[i] + myWord.length;. Remove that someText[i] from the mix and it'll work: j < myWord.length

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.