Why is my code only printing A's?


#1


var text = "Hey, how are you \
doing? my name is Axel. I would like to know more about \
you. So as I've already told you my name is Axel not Alex \
Axel a lot of people get confuse at the beginning."
var myName = "Axel"
var hits = []

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

if (hits.length === 0) {
    console.log("Your name wasnt in the text")
} else {
    console.log(hits);
}


#2

You need to add another = equal sign in your "if" statement. It should be 3 in total. :slight_smile:


#3

is still the same that doesn't change a thing


#4

You needed to add the three equal signs as I noted before then you needed to change this: hits.push(text[i]);

to this:

hits.push(text[j]);

Copy and paste it, it works.

var text = "Hey, how are you \
doing? my name is Axel. I would like to know more about \
you. So as I've already told you my name is Axel not Alex \
Axel a lot of people get confuse at the beginning."

var myName = "Axel"

var hits = []

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

if (hits.length === 0) {
    console.log("Your name wasnt in the text");
} else {
    console.log(hits);
}

#5

thanks the problem was the push.hits and the 3 equal sign makes no difference btw https://gyazo.com/2898a8634039e1d6937b57db9d191b89


#6

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