Exercise 7- Codecademy Says I'm Right but All Characters are Logged to Console, Not Just My Name!


#1



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


It tells me I passed, but instead of my name getting logged to the console, every single character in the "text" variable is. Why?


/*jshint multistr:true */
var text="Hello, my name is Shawn Spencer, thisis my partner, Lavender Goomes.\ I, Shawn Spencer, will solve your missing-persons case."
var myName="Shawn Spencer"
var hits=[]
for (var i=0;i<text.length;i++){if(text[i]==="S"){for(var s=i;s<i+myName.length;s++);}hits.push(text[i]);}
if(hits.length==0){console.log("Your name wasn't found")}else{console.log(hits)}


#2

the hits.push is not inside the the second for loop, and you should push text[s], you use the second for loop to loop over your name. Uhm.. both Shawn and Spencer are going to be found, that is a problem.


#3

I'm a little confused- now it returns a ton of blank characters: ' ', ' ', ' ', ' ', etc.
Here's my code now:

`*jshint multistr:true */

var text="Hello, my name is Shawn Spencer, this is my partner, Lavender Goomes.\ I, Shawn Spencer, will solve your missing-persons case."
var myName="Shawn Spencer"
var hits=[]
for (var i=0;iif(hits.length==0){console.log("Your name wasn't found")}else{console.log(hits)}

Also, why is "Shawn" and "Spencer" both being found a problem? That's the name I'm searching for, right?


#4

now you made your code a whole lot worse, lets go back to the first code:

I recommend to add more indention to your code, so we can actually see something.

your second for loop:

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

uhm... the semi-colon after ) is very dangerous, it prevents the loop from running properly. Also, just like your first for loop, you should use {} to indicate what is part of the second for loop:

for(var s=i;s<i+myName.length;s++){
   // push hits here
}

well both start with a S so both will be found, you could for example build a loop to check all characters, you could use substring, or you could check for the last letter (myName.length -1)


#5

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