Victory! I got the solution for printing exact name


#1


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

This is the solution for printing only "Eric" not "Eddie".

`

var text = "Blah blah blah blah Eddie blah blah Eric \
blah blah Eddie blah Eric blah blah Eric blah blah \
blah blah Eddie blah blah Eric";
var myName = "Eric";
var hits = [];

for(var i = 0; i < text.length; i++) {
if (text[i] === "E" & text[i + 3] === "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)
}

output :- [ 'E', 'r', 'i', 'c', 'E', 'r', 'i', 'c', 'E', 'r', 'i', 'c', 'E', 'r', 'i', 'c' ]

my tropie please


#2

okay, but if you really want a challenge, make it in such way that if we change text and myName, the code still works (so it can also distuinqish between David and Daniel, to name another example)


#3

its ok-----------------------------------


#4

but what if we want to do:

var text = "hello David, how is Daniel doing?"
var myName = "David"

how can we make the code in such a way, that its flexible? So that we only have to change text and myName, that your code then still works? And daniel and david is just one example. It could also be Julia and Josh.


#5

I will accept this challenge now.

var text = "hello David, how is Daniel doing?";
var myName = "David";
for(var i=0; i<text.length; i++) {
if (text[i]===myName[0] & text[i+4]===myName[4]) {
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);
}


#6

sorry bro challenge is still incomplete,
you just change the index value in for loop ,which is only work for David and Daniel,
and it seems u also not check your code, because there is error in your code.
The challenge is complete if this code work for any given name.
for example :- "hello Aditti, how is Adittya doing?" (your code is not work for this one)
Best of luck


#7

Now challenge is complete with this

var hits = [];

var text = "hello Aditti, how is Adittya doing?";
var myName = "Aditti";
for(var i=0; i<text.length; i++) {
if (text[i]===myName[0] & text[i+(myName.length/2)]===myName[myName.length/2]
& text[i+(myName.length-1)]===myName[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);
}

output :-[ 'A', 'd', 'i', 't', 't', 'i' ]

in this you can also try another name of your choice.


#8

@sonu1987, this only works for david and daniel. That was just another example.

@warsi3474, your solution is flexible :slight_smile:


#9

Yup, you are right bro.
I do not pay attention towards the flexibility of text and myName.

Congrats to warsi3474. That he created a flexible code for text and myName.
My full credit goes to warsi3474.:wink:


#10

I hate to ruin the party but;
var text = "hello Aditti, how is Abitti doing?";
for example would not work. I'm working on a solution at the moment and I'll keep you updated. Very close though.


#11

I do believe, unless anyone can prove otherwise, I have created the ultimate code that can take any string and find the exact word you want every time;

var text = "Blah blah blah blah Eddie blah blah Eric blah blah Eddie blah Eric blah blah Eric blah blah blah blah Eddie blah blah Eric";

var myName = "Eric";

var hits = [];

var test = true;

for (i = 0; i < text.length; i++) {
    
    if (text[i] === myName[0]) {
        
        for(var j = i; j < i + myName.length; j++) {
            
        if (text[j] !== myName[j-i]) {
            
                test = false;
                break;
                
            }
            
        }
        
        if (test === false) {
            
            test = true;
            
        } else {
                
            for(var z = i; z < i + myName.length ; z++) {
                
                if (text[z] === myName[z-i]) {
                    
                    hits.push(text[z]);
                    
                }
            }
        }  
    }
}


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

Sorry for the probably over dramatic layout, that's how I like to arrange my code. Saying that the code is still the same. Please do tell me if I'm wrong, but I think that one should indefinitely work.


#12

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