This is getting painful


#1

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

People, I've hit a mental wall here. What is going on. Sifted through all similar threads to find a solution but my pea brain isn't engaging with this one.


/*jshint multistr:true */

var text = "blah blah blah Giles blah blah blah blah blah Giles blah blah";
var myName = "Giles";
var hits = [];

for(var i = 0; i < text.length; i++) {
    if (text[i] === 'G') {
    for(var j = i; j < i + myName.length; j++) {
        hits.push(text[j]);
    }
}
}

if(hit.length === 0) {
    console.log("Your name wasn't found!");
} else {
    console.log(hits);
}


#2

Spelling error in this line:
if(hit.length === 0) {
console.log("Your name wasn't found!");

Should be:
if(hits.length === 0) {
console.log("Your name wasn't found!");

Also you are missing a parenthesis here:
or(var j = i; j < i + myName.length; j++) {

Should be: for (var j = i; j<(myName.length + i); j++) {

Hope this helps! :slight_smile:


#3

i agree with the misspelled hit(s) variable inside the if condition, but the the brackets around i + myName.length aren't needed, it is only one mathematical operation you do, so i don't see why you need brackets?


#4

Wasn't sure about those either. Was just looking through my own code which worked. Was just an advice :slight_smile:


#5

That worked guys - so thank you!


#6

I would imagine that WITHOUT the brackets it would read as j less than i, and then add the myName.length, instead of reading as j less than myName.length + i

But as I'm pretty new into all of this, I'm not sure. But glad it worked for you :slight_smile: