"Victory!". But I feel like a failure


#1

My code still runs but I don't get the output of how many times my name shows. This is what i get back when i run my code:
"[ 'H',
'i',
',',
' ',
'I',
'\'',
'i',
',',
' ',
'I',
'\'',
'm',
',',
' ',
'I',
'\'',
'm',
' ',
' ',
'I',
'\'',
'm',
' ',
'B',
'I',
'\'',
'm',
' ',
'B',
'r',
'\'',
'm',
' ',
'B',
'r',
'o',
'm',
' ',
'B',
'r',
'o',
'o',
' ',
'B',
'r',
'o',
'o',
'k',
'B',
'r',
'o',
'o',
'k',
's',
'r',
'o',
'o',
'k',
's',
'.',
'o',
'o',
'k',
's',
'.',
' ',
'o',
'k',
's',
'.',
' ',
'T',
'k',
's',
'.',
' ',
'T',
'o',
's',
'.',
' ',
'T',
'o',
'd',
'.',
' ',
'T',
'o',
'd',
'a',
' ',
'T',
'o',
'd',
'a',
'y',
'T',
'o',
'd',
'a',
'y',
' ',
'o',
'd',
'a',
'y',
' ',
'I',
'd',
'a',
'y',
' ',
'I',
',',
'a',
'y',
' ',
'I',
',',
' ',
'y',
' ',
'I',
',',
' ',
'B',
' ',
'I',
',',
' ',
'B',
'r',
'I',
',',
' ',
'B',
'r',
'o',
',',
' ',
'B',
'r',
'o',
'o',
' ',
'B',
'r',
'o',
'o',
'k',
'B',
'r',
'o',
'o',
'k',
's',
'r',
'o',
'o',
'k',
's',
',',
'o',
'o',
'k',
's',
',',
' ',
'o',
'k',
's',
',',
' ',
'a',
'k',
's',
',',
' ',
'a',
'm',
's',
',',
' ',
'a',
'm',
' ',
',',
' ',
'a',
'm',
' ',
'g',
' ',
'a',
'm',
' ',
'g',
'o',
'a',
'm',
' ',
'g',
'o',
'i',
'm',
' ',
'g',
'o',
'i',
'n',
' ',
'g',
'o',
'i',
'n',
'g',
'g',
'o',
'i',
'n',
'g',
' ',
'o',
'i',
'n',
'g',
' ',
't',
'i',
'n',
'g',
' ',
't',
'o',
'n',
'g',
' ',
't',
'o',
' ',
'g',
' ',
't',
'o',
' ',
'p',
' ',
't',
'o',
' ',
'p',
'r',
't',
'o',
' ',
'p',
'r',
'a',
'o',
' ',
'p',
'r',
'a',
'c',
' ',
'p',
'r',
'a',
'c',
't',
'p',
'r',
'a',
'c',
't',
'i',
'r',
'a',
'c',
't',
'i',
'c',
'a',
'c',
't',
'i',
'c',
'e',
'c',
't',
'i',
'c',
'e',
' ',
't',
'i',
'c',
'e',
' ',
'l',
'i',
'c',
'e',
' ',
'l',
'e',
'c',
'e',
' ',
'l',
'e',
'a',
'e',
' ',
'l',
'e',
'a',
'r',
' ',
'l',
'e',
'a',
'r',
'n',
'l',
'e',
'a',
'r',
'n',
'i',
'e',
'a',
'r',
'n',
'i',
'n',
'a',
'r',
'n',
'i',
'n',
'g',
'r',
'n',
'i',
'n',
'g',
' ',
'n',
'i',
'n',
'g',
' ',
'c',
'i',
'n',
'g',
' ',
'c',
'o',
'n',
'g',
' ',
'c',
'o',
'd',
'g',
' ',
'c',
'o',
'd',
'e',
' ',
'c',
'o',
'd',
'e',
' ',
'c',
'o',
'd',
'e',
' ',
'f',
'o',
'd',
'e',
' ',
'f',
'o',
'd',
'e',
' ',
'f',
'o',
'r',
'e',
' ',
'f',
'o',
'r',
' ',
' ',
'f',
'o',
'r',
' ',
'a',
'f',
'o',
'r',
' ',
'a',
' ',
'o',
'r',
' ',
'a',
' ',
'f',
'r',
' ',
'a',
' ',
'f',
'e',
' ',
'a',
' ',
'f',
'e',
'w',
'a',
' ',
'f',
'e',
'w',
' ',
' ',
'f',
'e',
'w',
' ',
'h',
'f',
'e',
'w',
' ',
'h',
'o',
'e',
'w',
' ',
'h',
'o',
'u',
'w',
' ',
'h',
'o',
'u',
'r',
' ',
'h',
'o',
'u',
'r',
's',
'h',
'o',
'u',
'r',
's',
'.',
'o',
'u',
'r',
's',
'.',
undefined,
'u',
'r',
's',
'.',
undefined,
undefined,
'r',
's',
'.',
undefined,
undefined,
undefined,
's',
'.',
undefined,
undefined,
undefined,
undefined,
'.',
undefined,
undefined,
undefined,
undefined,
undefined ]
"

What exactly did I do wrong? This lesson was really confusing for me.

/*jshint multistr:true */

var text = "Hi, I'm Brooks. Today I, Brooks, am going to practice learning code for a few hours." 

var myName= "Brooks";

var hits= [];

for(var i=0; i<text.length; i++){
    if (text[i]==="B");
        for (var j=i; j< i+myName.length; j++)
    hits.push(text[j]) }
    
    if (hits <0){
        console.log("Your name wasn't found");}
    else{ 
        console.log(hits);
    }

#2

Ok, well you didn't do too much wrong, which is good. For example;

for (var j=i; j< i+myName.length; j++)
hits.push(text[j]) }

this should look like the following:
for(var j = I; j < i + myName.length; j++) {
hits.push(text[j]);
}

the last thing you did wrong, you put
if (hits <0){
instead of
if(hits === "0") {

this should fix your problems! if not please reply to me and tell me what it says.


#3

So all you did in the first correction was put spaces between everything, add the "{}" and change the first "i" to "I". I understand why you would want the spaces to make it more legible.

This is what it puts out:
SyntaxError: Unexpected end of input

and just incase, here's my code again:

/*jshint multistr:true */

var text = "Hi, I'm Brooks. Today I, Brooks, am going to practice learning code for a few hours."

var myName= "Brooks";

var hits= [];

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

if (hits <1){
    console.log("Your name wasn't found");}
else{ 
    console.log(hits);
}

#4

my code is exactly the same as yours. the only difference is me putting if(hits === "0")


#7

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

The if statement lacks a body, and ends with the semi-colon (which it shouldn't). That makes the inner loop execute on every value for i. Check the braces.


#8

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