My second loop print out numbers instead of my name?


#1

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


my code does not push letters related to my name to the hits array, instead it seems to calculate the total number of letters in my name and print it out , in this case number 15 gets printed out. I checked and rechecked again for any missing item in my code but I am lost here .. help !

text = " I am going to work, Karim / do you need anything before I go? / by the way Karim there is food in the fridge and dont stay late playing games./ Karim just do your work and train to be the best programmer in the world. ";
var myName = "Karim";
var hits = [];

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]); } } };

#2

This string is the problem because it turned 98 % of your code into a string. :smile:

Either make it a multi line string or shorten the string. :slight_smile:


#3

multistr:true;
var text = " I am going to work, Karim / do you need anything before I go Karim Karim ";
var myName = "Karim";
var hits = [];

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]); } } };

alright I did both things, it continues to display the number 15 , oddly enough when I completed the following exercise "Log it!". it displayed the letters as the first exercise said it would. does this means I had to complete the last piece (adding the console.log(hits) ) so that the results displays normal??


#4

One thing i observed on your code, you used ( / ) slash instead of ( \ ) backslash to wrap your string to the next line. We are using backlashes ( \ ) to avoid long lines.


#5

I did rectify this, yet at this point it still displays number 15, of course that is before I add the console.log(hits) part and then it successfully displays my name's letters.


#6

multistr:true ;
text = " I am going to work, Karim \ do you need anything before I go? \ by the way Karim there is food in the fridge and dont stay late playing games.\ Karim just do your work and train to be the best programmer in the world. ";
var myName = "Karim";
var hits = [];

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]); } } };

here is my code with your correction suggestions, but still numbers ..


#7

Have you tried refreshing your browser? i dont see anything wrong on your code except the / you used instead of ( \ ). i also tried your code on my own console and all things worked smoothly.


#8

well I refreshed the browser, changed from firefox to chrome , and its the same ...
so I did some experimenting, I went back to the first exercise in this section "what you will be building" and I erased the console.log(hits) part .. and it displayed 16 instead of the letters in Eric's name . I do believe that the code at this point just calculate the total number of your name's letters in the whole text. ?


#9

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