Search text for your name - Log it


#1

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


Code passed, got the "Start Next Lesson" go ahead. BUT, the print out was 30 empty brackets [ ] one on top of the other. That can't be correct.


/*jshint multistr:true */
var text = "kjbf cfh;Linda irwbldj lib, Linda v biw lb\ b cjbvjb, Linda fboegg bv";
var myName = "Linda";
var hits= [];

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


#2

We initialize j to be the current position of i at match found index.

for (var j = i; j < i + myName.length; j++)

#3

Why did 30 [ ] log to the console?


#4

This should have been a comparison, not an assignment.

if (hits.length === 0) {

#5

I made the recommended changes to my code. Now it prints out: 9 "Your name wasn't found!"; 19 [ ' ' ]; 25 [' ' , ' ' ]; and 15 [ ' ', ' ', ' ' ].

I am officially bumfuzzled!

LO26

PS: I deleted the environmental mention on my signature per your suggestion. As you so rightly noted, this is not personal correspondence. You might want to re-think your picture -- introducing a baby seems quite personal, maybe better for Facebook.


#6

I like to point out one or two mistakes at a time, and bring up others as the earlier ones are repaired. The above is code that should follow the loop, not be included in it.


#7

Personal if was only yesterday. That picture was taken nearly 40 years ago. My youngest grand child is going on 3 years.


#8

HAHAHAHAHAHA! My apologies!

How long have you been coding? I'm a late starter, late based on age, but I'm enjoying learning, always have . . . .
LO26


#9

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