Improve search 6. Log It!


#1


Code is working fine and it logs my whole name however it does it 8 times. Is there a way to mak it log just once?


var text = "text tex te t text tx ts Ryan tes ts tex Ryan";
var myName = "Ryan";
var hits = [];

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

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


#2

here:

hits.push(myName);

not push myName, you make a second for loop so you can push each letter into your array


#3

You mean hits.push(text[j]); ?

It logs each character twice though: [ 'R', 'y', 'a', 'n', 'R', 'y', 'a', 'n' ]


#4

your name is presence twice?


#5

Oh I see what you mean. Thanks for the help anyway.


#6

it seems you want your program to behave different from what the exercise suggest


#7

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