Is the output correct?


#1

I followed all instructions and logically the output should be "some Lala text Lala", but I am getting
"some L text L" and my hits look like this [ 'L', ' ', 't', 'e', 'L', undefined, undefined, undefined ] Can somebody please explain what I am doing wrong?


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


The output is not what it is expected to be


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


#2

I just realized that hits was supposed to have the 4 symbols after the "search" finds an L in text.
hehe I expected the Ls in text to be replaced with "Lala"


#3

The inner loop is going out of range which is undefined (doesn't exist). Add some more characters to the text so there are at least three letters after the last L.


#4

Now I am curious if I actually CAN do that - replace the L with the full name. What string methods would I need?


#5

Use lots of text for the best results.

text = "In diam diam, molestie Lala nec ullamcorper id, condimentum \
id mauris. Sed lacinia libero Lala vitae massa hendrerit vitae porttitor \
mauris porta. Lin Praesent Lala dapibus vestibulum semper."

replace the L with the full name. What string methods would I need?

None. We would never think of mutating the data string that is defined by the program.


#6

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