My code does not work!


#1



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

Oops, try again. There was a problem with your syntax.


var text = "Hey, I'm Amber. Amber means beautiful rock \ or something like that.";
var myName = "Amber";
var hits = []

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

}

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


#2

Never mind!!! I had 1 too many curly braces :smiley:


#3

Your second for loop will go over your name letter by letter, which results in a lot of hits in your case, for each instance of your name, you push 5 ambers into your hits array. You could also do:

for (var l = i; l < i + myName.length; l++) {
   hits.push(text[l])
}

l will hold the index of the a letter of your name while it run, and by using this number to access your text variable, you can push the letters into your hits array, one by one


#4

I don't understand of writing this code.Would you explain in simple words?


#5

I did wrote two if not more extensive explanations of the second for loop, have you read those? Read those, or of someone else, and then you can come for a specific question, such answers are very time consuming