Victory! - Lesson passed but Console output is blank


#1


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

So at this point, it is allowing me to pass Lesson 7 Victory, but the code does not seem to be outputting to the Console correctly.

When the code is run exactly as printed below, the Console simply outputs a blank set of brackets "[ ]", and the lesson allows me to pass.

I thought that perhaps this was still correct, and then changed the contents of my "text" variable to not include my name at all. In theory the If statement starting on line 27 should then tell the Console to output "Your name was not found." However, even with this change to the contents of "text", the Console still only outputs the blank set of brackets "[ ]". The lesson still allows me to proceed.

So....now I'm confused. Did my code not work correctly in the first place? When my code scans through "text" comparing it to array "hits", should the Console be outputting anything? And why, when I edit the contents of "text" to no longer include "myName", does my code not correctly execute the If statement telling it to output "Your name was not found"?

Many thanks for any help.


/*jshint multistr:true */

var text = "My dog Oliver is dying for a treat\ he sounds hungry \ My name is Connor by the way \ that's right I said Connor \ y'all ready for this \ Connor Connor Connor \ y'all ready for kiss \ give Oliver his treat dammit";



var myName = 'Connor';



var hits = ['C','o','n','n','o','r'];



for(var i = 0; i < text.length; i ++){
   if (text[i] === 'C'); {
    for(var j = i; 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

you have semi-colon here:

if (text[i] === 'C');{

which means the for loop won't execute properly. Also, i would use two equal signs here:

if(hits.length == 0)

you want to check if the length is 0, not assign something. I don't understand why your hits array already contains hits, i would it to be empty, to store any hits in the text string


#3

Your array hits should be left empty.

Yours: var hits = ['C','o','n','n','o','r'];

should read

var hits = []

your for loop should populate the array if it finds a match, you shouldn't be populating it in advance.