Improve search 6. Log It!


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++) {

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




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


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

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


your name is presence twice?


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


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


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