7- Victory Not sure if correct


#1

i ran this code;
/*jshint multistr:true */

var text = "hello my name is rob, what is your name? also rob? how boring is this throb!"
var myName = "rob"
var hits = []

for (var i = 0 ; i < text.length ; i++){
if (text[i] === "r"){
for(var j=i; j < (i+myName.length); j++)
hits.push(myName)
}}
if(hits === 0){
console.log("you have no hits")
}
else{
console.log(hits)
}

and it returned with my name (rob), a lot of times [ 'rob',
'rob',
'rob',
'rob',
'rob',
'rob',
'rob',
'rob',
'rob',
'rob',
'rob',
'rob',
'rob',
'rob',
'rob' ]


#2

You should do:

hits.push(i);

Instead of

because it will put the letter it found instead of your name for each letter.


#3

how would i know when to use myname and i/j


#4

What it wants you to do is append a hit to the list, so instead of appending R-O-B when found, your code would append ROB-ROB-ROB for each letter it found, so essentially you aren't appending the letters you found, but your name, which isn't the intended result.


#5

if i use text[j] it ran the code correctly


#6

Yes, as j = i, I just use i as j will not work if the hit is the first character in the string.