Is the end result suppose to be a number?


#1


Replace this line with your code.


#2

yep, its the number of hits found. In the next exercise you will work on how to display the hits found


#3

That's fine and all but the result renders 12 hits rather then 4, for 'Alex'. I have been going over the code for 4hrs and can not find why that is.


Replace this line with your code.


#4

unless you provide me with code, i can't tell you that either


#5

How can I do that ? Thanks


#6

you can copy paste your code to the forum


#7

var text = 'Hi, my name is Alex.\
Alex is short for Alejandro.';

var myName = 'Alex';

var hits = [];

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


console.log(hits);

#8

your second for loop is designed to to push single letters to hits, not your name in one go.

j matches the indexes of your name in text, so you can do: text[j] then you will see what is pushed: 2x Alex 1x Alej.


#9

var text = 'Hi, my name is Alex.\
Alex is short for Alejandro.';

var myName = 'Alex';

var hits = [];

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

console.log(hits);
[/quote]

Like so: [ 'A', 'l', 'e', 'x', 'A', 'l', 'e', 'x', 'A', 'l', 'e', 'j' ]


#10

this should be the output, yes :slight_smile:


#11

I think I understand. I can't believe I've been working on the same problem from 5hrs straight. It was driving me crazy! Thank you for your help.


#12