Search Text for your name JS - Success not visible


#1

Hi there!

I'm getting a pass on this sending me to the next step however in neither phase is my name being printed out via the console.log. Any idea why I'm getting an empty array [ ] as my result?

How can I get this to print my name in the console display?
Thanks in advance for for any help!

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

/*jshint multistr:true */
var text = "hi my name is Alex and Alex is the name!";
var myName = "Alex";
var hits = [""];

for (i=0; i < text.length; i++){
    if (text[i] === "A"){
        for (j=i; j < myName.length + 1; j++)
hits.push([j]);
}
}

if(hits.length = 0){
    console.log("Your name was not found!");
} else {
    console.log(hits);
}

#2

Change the 1 to an i so it iterates over all four letters.


#3

Hi @mtf, I appreciate the reply! However i'm still getting a blank array printed. Any chance it's a bug in CodeCademy? ..or are you getting the name printed into an array when you run??

Thanks!

I think i understand the logic but this is what I'm seeing:


#4

This should be raising a syntax error in the console. We cannot make an assignment in a conditional expression.

hits.length === 0

Be sure you start with an empty array.

var hits = [];

#5

@mtf, nice! I made your suggested changes and it's printing now. Thank you!

This poses a new question as it's printing the first letter postion of the array (name.length times) instead of the actual letters. I'll dig around for solutions :wink:

Appreciate the quick help!!


#6

Sorry, I tend to mete out answers one at a time to give the learner time to discover their mistakes. The last thing to cover is what is being pushed to the hits array. As you have discovered it is pushing the index, not the letter.

text[j]

will be the letter at index j.


#7

Works like a charm! Thanks again @mtf!!
Sure to see you around these threads :slight_smile:

Alex


#8

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