This forum is now read-only. Please use our new forums at discuss.codecademy.com.

561 points
9a5f6d806d2cc32d82f3350ab39e190c?s=140&d=retro
Submitted by
Carl
almost 4 years ago

The Eric example

The text on the left says:

Now, as we mentioned, this system
isn't perfect. For instance, if the
paragraph contains both "Eric" and
"Eddie", we'll see this in our hits
array:

['E','r','i','c','E','d','d','i','e'];

I expect the array would look like: ['E','r','i','c','E','d','d','i'], without the last letter of Eddie.
Since each time the capital 'E' is found, four letters (length of 'Eric') are copied to the array.
What is going on?


1 vote

permalink

same output:

[ 'E', 'r', 'i', 'c', 'E', 'd', 'd', 'i' ]

code:

var text = " Eric and Eddie"
var myName = "Eric"
var hits = []

for(var i = 0;i < text.length;i++){
    if(text[i] === "E"){
        for(var j = i;j < (myName.length+i);j++){
            hits.push(text[j])
        }
    }    
}

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

335 points
78bde38f802cb356151a46df34fd9ff8?s=140&d=retro
Submitted by
tra201
almost 4 years ago


0 votes

permalink

It's silly how this little mistakes can get us really confused as at this stage we are all learning.

Someone should correct that.

myName.length in Eric's case = 4
myName.length in Eddie's case = 5

So as Carl and tra201 suggest, the result would not be ['E','r','i','c','E','d','d','i','e'] but ['E','r','i','c','E','d','d','i']

I am happy I found this post as I was starting to get crazy and very frustrated thinking that I wasn't able to understand it.

622 points
Dc35adf7c8dbdc4bf7362eb3e70fc39b?s=140&d=retro
Submitted by
Bernardo San Juan
over 3 years ago