And if your name have an starter letter in the middle os its? allan - "a"ll"a"n


#1

Search Text for your Name!

If your name is like mine (allan), this exercise wont run correct until you change your name :slight_smile:


var text = "my  is alln, your  is alln, her  is alln";

var myName = "alln";

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 wasn't found!");
} else {
    console.log(hits);
}

#2

you could change your name to Allan and then change your if condition to:

if (text[i] === "A")

this will work, given string comparison is case sensitive.


#3

Thank you, I'll try it on.


#4

you can also replace "a" with myName[0]


#5

true, this will be better (given you don't hard code the letter)


#6

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