Didn't push the correct letters into array but still passed



So the code I wrote is pulling letters from the text but it is not pulling the correct number of them before finding the next instance of the first letter of my name. Also it only pulled from the first instance of the first letter of my name. No errors were given and the exercise still passed me.

/*jshint multistr:true */

var text = "So I was walking down the street when I saw what looked like Scott walking towards me. So I stoppped to stare when I noticed I was standing in the middle of the street. Seeing me standing in the middle of the street Scott started to run to save me from the oncoming car I didn't notice. Just as I was about to be pancaked into the asphalt Scott dove and knocked me out of the way.";

var myName = "Scott";
var hits = [];

for (i = 0; i <= text.length; i++) {
    if (text[i] === "S") {
       for (j = i; j <= myName.length + 1; j++) {

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


in your text string, So will also be found, use lowercase s for so.

there is a problem with your second for loop:

for (j = i; j <= myName.length + 1; j++)

why use <=? < will do, since the length of your name is 5, lets say the first letter of your name is at index 4, your name is at: 4,5,6,7,8. and 4 + 5 is 9, so you don't want to include 9

Speaking of which, still your second for loop, why myName.length + 1? It shouldn't be plus one, what do you think it should be?


Thanks for the reply.

I think I did <= because I wanted to count the last letter of my name. Guess I didn't think that through properly. I chose the +1 as a guess and the lesson let me through as if it was correct so I didn't question it. I played around with the code and changed the +1 to +i and it runs perfectly now. Well as far as the lesson dictates it does.

I don't mind it picking up other things due to the other "S"s. I want to leave those in there so when I tinker with it to only find my name I can make sure it is working properly. Thanks for your help.


sounds like you solved it :slight_smile:


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