How do I improve the code to add some validation? Help me, please!


#1

https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/7?curriculum_id=506324b3a7dffd00020bf661

I finished the "Search Text for Your Name" excercize but I want to improve the code. My name is Victor and I want the code not to print out anything else than my actual name even if there is another V in the text.
That is what I tried to do: i tried to add another loop that is going to chech next few letters after V is found in the text and compare it to the letters in my name and if, for example, the 2nd or 3rd etc. letter is differ from the letter with the same number in my name, the hits would become blancked array again. But for some reason it didn't work and in any case I only have "r" at the end so, as I understand, my code is just deleting everything before it (even when it comes to my name). However if you take out hits = []; part, it is gonna work, but with no validation...

var text = "Hey, how are you doing, Valentine? My name is Victor.";
var myName = "Victor";
var hits = [];

for (var i = 0; i < text.length; i++) {
    if (text[i] === myName[0]) {
        for (var c = i; c < (i + myName.length); c++) {
            for (var e = 0; e < myName.length; e++) {
                if (text[c] === myName[e]) {
                    hits.push(text[c]);
                }
                else {
                    hits = [];     
                }
            }    
        }    
    }    
}

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

#2

sounds complicated, why not simply to check for the last letter as well?

if (text[i] === myName[0]) {
if(text[i+myName.length-1] === myName[-1]){

it is very unlikely the last letter happens to match just another random word


#3

Sounds interesting. Thank you. I was thinking about it and i guess we can do it even simplier. Why not use substring command. I checked, it works well. :grinning:

var text = "Hey, how are you doing, Valentine? My name is Victor.";
var myName = "Victor";
var hits = [];

for (var i = 0; i < text.length; i++) {
    if (text[i] === myName[0]) {
        if (text.substring(i,i + myName.length) === myName) {
            hits.push(text.substring(i,i + myName.length));
        }    
        else {}
    }    
}

if(hits.length === 0) {
    console.log("Your name wasn't found!");
} else {
    console.log("Your name " + hits + " was wound in the text, it begins at the position of " + i);
}

#4

because sometimes it is fun to try and solve a problem without to many build in function? But yes, it will work


#5

Can you explain what the -1's are doing in this?


#6

oops, i am confusing js and python syntax, that should be:

if(text[i+myName.length-1] === myName[myName.length-1]){

which minus one? length starts counting at 1, string are zero indexed based (counting starts from zero), so you need -1 to compensate for this


#7

I see, so essentially it's so that it starts counting from 0, to make sure all the characters are covered.

Why does .length start counting from 1 and not 0?


#8

well, it also counts from zero actually:

"" // zero
"a" // one

but a index starts at zero with the first element

Length counts with the same principal you count in everyday life.


#9

I think it's a good idea to know at least approximately how something is implemented before using it. Otherwise you're just gluing things together that you don't know what they do and may end up with something that's slow and/or unreliable.

How could you compare that substring with your name without using the substring method? Or how could you create a function that behaves the same way? Could it be done faster by not creating a temporary string? What has to be done to create that string, how long does it take relative to the length of the original and the substring?

Also note that the name is already known, the information that hits is storing can be stored more compactly with just a single number and a single string (since it's a repeated string)


#10

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