7. Victory! Error


#1

I wanted to point out to anyone else that was confused. In the description for 7. Victory! it states:

"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'];"

This is actually incorrect. The code was setup to take the length of myName, in this case "Eric", and push the next characters in the text array up to the length of myName. That being said, while the For loop would initiate upon finding "Eddie" it would only catch "Eddi". The description is in error.

At least I hope it is. Otherwise I'm going to feel real stupid.


#2

If i read this, seems your are right.

I got a fun challenge for you if you like. How to overcome this problem that it doesn't push Eddi(e) to hits? Maybe check if length is good? Or maybe check the last letter also?

Just a challenge, if you like. I think it is a fun challenge, and it shows you master this part of the js track.


#3

So using only the tools that have been introduced in the tutorial thus far I would probably do something like this:

text = "Eric Eddie Eric asbfwer lsi Erig Erdn Ektn Erig";

var myName = "Eric";
var hits = [];
var count = 0;

// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
    count = 0;
	if (text[i] === myName[0]) { 
		for(var k = 1; k < (myName.length); k++){
		    if(text[i+k] === myName[k]){
		        count++;
		    }
		    if(count === (myName.length - 1)){
		        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);
}

It's not perfect but it makes certain to only grab your name. It would fail in situations where your name was contained with text like "Dan" being found in "Daniel."indent preformatted text by 4 spaces


6. Log it! incorrect result in Result tab
#4

This is extensive checking, this checks every letter. Nicely done, i would personally do this:

for(var k = 0; k < (myName.length); k++){
            if(text[i+k] === myName[k]){
                count++;
            }
            if(count === (myName.length)){

i changed k = 1 to k = 0, which means you can just check for myName.length rather then myName.length -1. Just a small optimization. Don't get me wrong, there is nothing wrong with your code, just showing a alternative. Never pass an opportunity to learn something.


#5

While on the subject, i would cut corners. I would simple do:

text = "Eric Ericson Eddie Eric asbfwer lsi Erig Erdn Ektn Erig";

var myName = "Eric";
var hits = [];
var count = 0;

// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
    if (text[i] == "E" && text[i + myName.length -1] == "c" ){
        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);
}

i added and (&&) to the if statement, it now checks if the last letter is also a match. Just a thought, what do you think?


#6

Yeah I thought about some of these as alternatives. You're correct, my first method was extensive. There are probably hundreds of ways to do this and better ways with different library tools. This last one would be significantly faster than my previous method.


#7

And the last method (my method) gives you the advantage that you can also relativity easy to prevent Ericson is added to your hits, i know how, can you think of a good solution? If you like, all these challenges are just bonusses


#8

Hi there, I was interested in how to prevent Ericson from being printed? I have tried the task but can't figure it out.


#9

and my method to also check the last character of the name? Didn't work either?


#10

This is how I'd prevent Ericson from being added. However, it does add Eric from Ericson and I had to use .splice to remove Eric(son) from the final array. I also did a "how many times" your name was found function at the end since I know the only info inside the hits array is my full name.

for (i=0; i < text.length; i++) {
if (text[i] === myName[0]) {
var j = 0;
for (var k = i; k < i+myName.length; k++) {
if (text[k] === myName[j]) {
hits.push(text[k]);
j++;
}
}
/*following if() runs if j did not reach the full length of myName, this only occurs when the text[ ] and myName[ ] did not match up exactly for my whole name length. Bran stops when j=4, which is less than myName.length which is 7.*/
if (j < myName.length && j > 0) {
hits.splice(-j, j);
j=0;
} /* hits.splices( moves the cursor -j elements from the end of the array, and removes the next j entries). Its like Bran never existed. I had to then set j=0, otherwise this if() would continue to splice (delete entries from hits[ ]) indefinitely.*/
}
}

if (hits.length === 0) {
console.log(myName + ", your name wasn't found!");
} else {
console.log(hits);
console.log(myName + " was found " + hits.length/myName.length + " times.");
}


#11

var text = "brando alskdf alsdknfxcvl sadlfkj xzcm,vnsadf lkxcvnmadsf brandon alsdkjf laksBrandonlkasdf bran alsdkfj brandon";

this was the text I was searching through. I was using myName="brandon"