When i run my code, i get a number as a result. Is that ok?


#1


What is supposed to be the end result of this code? Mine shows number. I get the green light but i don't know if that is ok. :slight_smile:


#2

It depends on the lesson, post a link to the lesson and your code.


#3

https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661

var text = "Nikola Nikola Nikola Nikola";
var myName = "Nikola";
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]);
        }
    }
}

It looks like it just counts strings. This code will show "24", if i add one more "Nikola", will be 30.
If a add some other text replacing "var text" it will show:
". It looks like your second 'for' loop isn't pushing values to the hits array."


#4

You should be able to add more text without getting an error message. In any case (as an experiment), add a line at the end of your code that says:

console.log(hits);

And see what displays in the console.


#5

Youre somehow getting the length of hits, its correct, but it looks like youre also getting spaces. With your string being just your name, its not really challenging your script. Look at this post to see what it should be doing.


#6

He's not getting the spaces. 'Nikola' is 6 characters, it appears 4 times in the text variable and he said the crt displays a 24. With the spaces, the total would have been 27.


#7

It only works when i type in "var text" as i did. If I type any other string, it doesn't.
This is how my code looks like when i add console.log. But only with same var text i typed above.
[ 'N',
'i',
'k',
'o',
'l',
' ',
'N',
'i',
'k',
'o',
'l',
'a',
'N',
'i',
'k',
'o',
'l',
'a',
'N',
'i',
'k',
'o',
'l',
'a' ]


#8

As an experiment, change your var text to "Nikola is a great coder" and run it.


#9

This is good. Can you explain to me why this happened?
This is the result:
[ 'N', 'i', 'k', 'o', 'l', 'a' ]


#10

Thanks for your reply, i was a bit confused, but i tried to read your comment couple of times to understand it. I got problem solved. :wink: Maybe it's more of a bug rather than a bad code.


#11

Youre right, i counted wrong. I subtracted one from my count of nikola because of index 0, so i counted 5 four times instead.


#12

I had a feeling it mightve been a little confusing, im sure the code example was all you needed from that post.

I believe youre getting a number because of codecademy. If you were to run your code in dev tools, or your browsers console window, it shouldnt show any number.


#13

Yes, the code was good, but it's much better with an explanation. :slight_smile: And it's probably CC.


#14

I left comments above, hope it helps, @emgo_dev's explanation is more thorough :slight_smile:


#15

Thanks a lot. I understand all of it. This is how i did it at the first, but it was a bug. :slight_smile:
You should blockquote your code in the comments because it shows "j<" like this "j&It;" :smiley:
Thanks once again. :wink:


#16

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