[Final step 6/7] Only returning name once- please can someone help!


#1

Hi,
I have been trying to write in an .indexof function to search just for my name but I don't think I've put it in the right place in my code, could someone take a look and maybe give me a hint?
In this case the name is 'Rome' and when I run the code the console returns [ 'R', 'o', 'm', 'e' ]
My code:

/*jshint multistr:true */
var text = "Rose went to Rome last year and saw lots of the tourist\ attractions of Rome. Roads in Rome were really busy";
var myName = "Rome";
var hits = [];
for (var i=0; i<=text.length; i++) {
if (i === text.indexOf("Rome")) {
for (j = i; j< (myName.length + i); j++)
hits.push(text[j]);
}
}
if (hits === 0) {
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}

Thank you!!


#2

Hi @rosepose

The indexOf() method returns the position of the first occurrence of a specified value in a string.

Well, in your "text" variable there are 3 "Rome" and the first is at index[13]. It's the only one who the indexOf() method will return.

New exercise : you have to find an other way to find it, hold on :wink: