Stuck at 'Your second 'for' loop'


#1

So I'm completely stuck at this section, and I've even tried copy pasting directly from the fixed code of other people on this forum who had the same issue and it just keeps crashing my browser. Any ideas what I can do? Here's the code I've been using:

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

#2

Your loop iteration variable of second for loop is j ?
(you will get one more error after you fix this)


#3

your second for loop, why i++, you use j to get from the beginning to the end of your name, to achieve this, increase j, not i

also, i contains the index (a number) of your name found in text, to actually push letters of your name, use j index value to retrieve letter from text, general syntax:

string[index]

#4

I'm sorry but I'm completely new to JavaScript and I didn't understand what you're telling me to do at all... any chance you could make the changes in my code so I can see what I'd need to change? Thanks!


#5

even this bit? where i am telling you to change i++ in your second for loop


#6

well i've changed it to j++ now if that's what you mean but it's still not working, now it's just printing a bunch of numbers


#7

good first step :slight_smile:

j is a number, but this number does match our indexes of your name in text

remember how can use indexes to get values from a string?


#8

I'm fairly sure we haven't learned about indexes yet at this point, and in the instructions it definitely doesn't mention indexes...


#9

you have, but for arrays, but it is the same for strings (exercise)

it mentions briefly:

This loop will make sure each character of your name gets pushed to the final array


#10

Ok so I've tried doing this now and it's not working either:

/*jshint multistr:true */
var text = "blah blah blah blah blah blah blah Julian \
blah blah Julian blah blah blah blah blah blah blah blah blah \  blah blah blah blah blah Julian blah blah blah blah blah blah  \
 blah blah blah blah blah blah blah Julian blah blah blah blah"
var myName = "Julian"
var hits = []
for (var i = 0; i < text.length; i++) {
    if (text[i] === myName[0]) {
        for (var j = i; j < (myName.length + i); j++) {
            hits.push(j)
            console.log(hits[i])
        }
    }
}

I really appreciate all the help you've been giving me but I would greatly appreciate it if you wouldn't mind sharing the final code? This is really confusing me and usually if I see the final product I can work backwards and understand why certain things have been done, but I'm afraid otherwise I'll be stuck on this exercise forever!


#11

but you are so close now? Here:

console.log(hits[i])

why do you use i for indexes here? We use the second for loop to find your name, j contains the right indexes

then all you need to do is push text[j] into your hits array


#12

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