Search for your name that ACTUALLY searches for it


#1

var text = "You can ignore the jshint... Allan for now. \
All that does is tell the console to Allan worrying about \
our use of backslash characters for wrapping long lines of text.";

var myName = "Allan"

var hits = [];

for (i=0;i < text.length; i++) { //goes through the text
    if (text[i]===myName[0]) { //checks the first letter
        for (j = 0; text[j+i] == myName[j]; j++) { //if it's true - checks the whole word
            if (j == myName.length - 1) { //and if the whole word is okay
               for (k = i; k < myName.length + i; k++) { //finally pushes that word
                   hits.push(text[k]);
                };
            };
        };
        
    };
    
};

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

This is my humble attempt to make this programm do what it claims to do (before that it grabbed the word "All", for example). But it feels a little clumsy, if there is a way to do it better — please tell me, I'm still a newbie.


#2

Hi here

1

if (text[i]===myName[0])

You could do it like that

if (text[i]=== "A")

Which is the first letter of your name.

2
And Then the second for loop inside the if statement you could do it like that

for (j = 0; text[j+i] == myName[j]; j++)

Firts part j = 0 should be j= i and the second part text[j+i] == myName[j] should j < myName.length + I so it should be like that

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

You should remove the second if statement and the third for loop

and inside this for loop

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

put

hits.push([text[i])

#3

This was my solution, which seems to work okay at checking the first three letters of a word to see if it's a match, then pushing the value of myName to the hits array.

This works fine for my short name, but for longer names you could end up pushing false positives to your array. How can I ask the program to check for as many letters as needed, if instead of having me set the variable myName, it was a user's answer to a prompt determining the string's length? Are there any other problems inherent to this approach?

Thanks for any pointers, as I'm still pretty new to JS!

var text = "Hello! My name is Kat, and I've been called Kat since I was a kid.\ I think \'Kat\' sounds better than \'Kathryn\' for someone my age.";
var myName = "Kat";
var hits = [];

for (var i = 0; i < text.length; i++) {
if (text[i] === myName[0]) { // if first letter is correct
for (var j = i; j < i + myName.length; j++) { // checks second letter
if (text[j] === myName[1]) { // if second letter is correct
for (var k = j; k < i + myName.length; k++) { // checks third letter
if (text[k] === myName[2]) { // if third letter is correct... then good enough!
hits.push(myName); // pushes value of myName to hits array
}
}
}
}
}
}

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


#4

Hi if you want it to works for your longer name you could just add it inside the variable myName just after you short name like that

var myName = "Kat Kathryn"

How can I ask the program to check for as many letters as needed?

You could do it like that

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

if instead of having me set the variable myName, it was a user's answer to a prompt determining the string's length?

you could just add a prompt You could it like that

var userAnswer = prompt("please type your name :  ")
var myName = userAnswer; 

Or

var var myName = prompt("please type your name :  ")

Are there any other problems inherent to this approach?

Maybe one , you should remember that the name you input should be inside the var text.

I hope I could help you.


#5

Thanks for the reply, wizmarco! Unfortunately, it doesn't look like that code is printing what I want it to. When I use

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

in place of my for loop, it prints

[ 's',
' ',
'K',
'a',
't',
',',
' ',
's',
'i',
'n',
'c',
'e',
' ',
'I',
's',
' ',
'a',
' ',
'k',
'i',
'd',
's',
'o',
'u',
'n',
'd',
's',
' ',
's',
' ',
'b',
'e',
't',
't',
'e',
's',
'o',
'm',
'e',
'o',
'n',
'e' ]

My original code above prints the user's name if the first three letters are found in the text, but that would return a false positive if the user's input were "betty", since my text contains the word "better". Alternatively, if the user inputs a name that's shorter than three letters, it always prints that their name can't be found, since I asked the code to check for three letters in the text.

My search function is semi-successful in how it's set up now, but without knowing the length of the user's input, I don't know how many times to search for the next letter. I need to figure out how to base the number of conditionals on the length of the input value.


#6

Hi @haxor789 can you help her with here code , thanks.


#7

Ok what @systemslayer67600 and @kat.timm try to do is to make sure that the actual name is added and no other words that happen to start with the same letter.

A third for loop might actually be a good idea but the problem in @systemslayer67600 code is that this for loop would only run once. And the whole meaning of loops is to write reoccurring statements shorter so a loop that only runs once would better be replaced with an if.

@kat.timm same here. In the end your loops only run once or worse pick a later letter that is also in your name. Maybe run this program with a longer name in:
http://pythontutor.com/javascript.html#mode=edit
to see what i mean.

You could of course chain && here:

if(text[i] == myName[0] && text[i+1] == myName[1] ...)

but that wouldn't be really dynamic. What about using a variable and a for loop here?

for (var i = 0; i < text.length; i++) {
    if (text[i] === myName[0]) { 
        var isMyName = true;
        for(var k=0;k<myName.length;k++){
            if(text[i+k] != myName[k]){
                isMyName = false;
            }
        }
        if(isMyName){
             //push name
        }
    }
}