Your second "for" loop


#1

Im such a newb....

Can anybody tell me what is wrong with my code here?

var text = "How are you doing today, /Kyle?"
var myName = "Kyle"
var hits = [];
for (i=0; i < text.length; i++){if(text[i] === "K"){for (j = i; j < myName.length; j++){
console.log(hits.push(myName[K]));


#2

Missing the offset, i.

j < i + myName.length;

#3

So now I am running this:

var text = "How are you doing today?"
var myName = "Kyle"
var hits = []
for (i=0; i <text.length; i++){if(text[i]=== "K"){for(var j = i; j < i + myName.length; j++){console.log(hits.push(myName[K]))}};

Im getting "Syntax Error: Unexpected end of input

I have taken a break for a couple days and lost all my brain smh


#4

Really tough to read and debug. The error message suggests there is a problem with braces (missing closing brace).

To make your code easier to read and debug, give at least the closing braces there only line.

for (var i = 0; i < text.length; i++) {
    if (text[i] === "K") {
        for (var j = i; j < I + myName.length; j++) {
            hits.push(text[j]);
        }
    }
}

The last brace is the one that was missing. See how much easier it is to spot?

Note that we will not be logging each time a character is pushed onto the array.

Aside: Be sure to end your line statements with semi-colons. for and if statements do not end with semi-colons. They are declarative so get pre-compiled at load time. LIne statements get compiled when they are run, so the semi-colon tells the interpreter where to stop parsing.


#5

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