Javascript 5. Your second "for" loop; get number istead of letters


#1



I get the code to run, but instead of letters (as expected in the instructions), I get the number 364 in the console window.. There isn't an error message,I just get a number istead of letters. Can someone explain why this happends, where in my code did I go wrong? Thanks!


//jshint multistr:true
var text ="Blah blah blah Habibie blah blah Habibie blah Habibe";
var myName = "Habibie";
var hits = [];

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


#2

this line:

if (text[i] === myName[0]);

remove the semi-colon, it interrupt the if clause.

as for the number, it is the value of hits, the codecademy console logs the last value/variable, in the next exercise you will work on printing your name


#3

The code looks fine maybe instead of j< (i +myName,length) just j < myName.length + i

but just to make sure can you
console.log(hits);
Just to see what comes up.


#4

I did as you said, and I've moved into the next excise, but it still prints out number, this time from 1-364.
It still runs, but it prints the numbers in the console window.

var text ="Blah blah blah Habibie blah blah Habibie blah Habibe";
var myName = "Habibie";
var hits = [];

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

if ( hits = 0 ) {
    console.log("Your name wasn't found!");
    }


else {
    console.log(hits);
}

#5

you didn't remove the semi-colon in the if condition like i suggested?

then here:

if ( hits = 0 )

you should check if hits.length equals (==) zero, equal requires two equal signs. a single equal sign means assign


#6

Thanks! Looks better now! I still get numbers, but atleast I now laog get my name printed out. The number thins, is it becous how CodeAcademy works? This is what I get know in the console window.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[ 'H',
'a',
'b',
'i',
'b',
'i',
'e',
'H',
'a',
'b',
'i',
'b',
'i',
'e',
'H',
'a',
'b',
'i',
'b',
'e',
undefined ]


#7

did you apply the above suggestion?


#8

Yes I did




#9

can you post an updated version of your code?


#10

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