6.7 What's wrong with my code?


#1

I tried to verify the hole name to avoid mistakes but my code counts "i" twice every time and I can't understand why. How do I fix it?

/*jshint multistr:true */

var text = "Vinícius e slej sjdubs dwjxiki \
eeeee svgafr Vi Vinícius wdk dckjcsk ebxdhjbx \
hjh Vinícius"
var myName = "Vinícius"
var hits = [];

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

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

#2

You don't need this:

It really doesn't have any use. SO:

You don't need this either.


#3

thank you ragezapper this helped me too


#4

like that only the fist letter is checked.. I was looking for a way to check the hole name


#5

Thank you! I got mixed up with the hits.push(text[j]) thingy, but reading this sorted it out for me. :sweat_smile:


#6

No, the function uses your myName array to find the rest of your name after it finds the first name of it. For example:

var text = "EFD Eli EFWD Eli EFSCJD NHBUW Eli"
var myName = "Eli"
var hits = [];

f

This and:

Helps the code to find the rest of the name after they find the first letter.