7. Can someone explain how to fine-tune the code


#1

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


var text = "Blah Blah Edwin Blah Blah Blah Blah Blah Blah \ Blah Blah Blah Edwin Blah Blah Blah Blah Blah Blah Edwin \ Blah Blah Blah Blah Edwin";
var myName = "Edwin";
var hits = [];

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


#2

Fine tune in which way?


#3

var r=text.match(/Daksh/gi);
console.log(r)

use the above line of code.
here "Daksh" is the string to be searched for.
"gi" means case sensitive search


#4

I see, but how to make the answer such as below:
(this is the result before I added var hits = text.match(/cabbage/gi); )

[ 'c', 'a', 'b', 'b', 'a', 'g', 'e', 'c', 'a', 'b', 'b', 'a', 'g', 'e' ]

for my code is:

/*jshint multistr:true */

var text = "healthy cabbage is healthy, thus the cabbage is healthy.";
var myName = "cabbage";
var hits = [];

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

if (hits===0) {
    console.log("Your name wasn't found!");
} else {
    var hits = text.match(/cabbage/gi);
    console.log(hits);  
};

and with this code, the current output is:

[ 'cabbage', 'cabbage' ]

Thank you very much for your reply :slight_smile:


#5

what is the question?


#6

I think the question is, how would you set this up to do an exact match search? I actually have the same question. I used the code from daks and it printed my name BUT it also printed all of the other text. I must have inserted it wrong or not removed something. Can you help?

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

/*jshint multistr:true */
var text = "bla bbla bla blba bll hi, how are you blba bla bla bla bla bla Kevin blab bla hi ahow bla bla bla bla bla Kevin";
var myName = "Kevin";
var hits = []
for (var i = 0; i < text.length; i++) {
if(text.match(/Kevin/gi)); {
hits.push(text[i]);
};
};

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


#7

if you use something like text.match or substring, you can simply do this:

var text = "bla bbla bla blba bll hi, how are you blba bla bla bla bla bla Kevin blab bla hi ahow bla bla bla bla bla Kevin";
var myName = "Kevin";
hits = text.match(/Kevin/g)
console.log(hits)

that is it. text.match does everything for you, Kind of beats the point, by building the loops yourself you get a better understanding of you could do this yourself


#8

Hi, the question is how to make the result that I want is such:

[ 'c', 'a', 'b', 'b', 'a', 'g', 'e', 'c', 'a', 'b', 'b', 'a', 'g', 'e' ]

I have added either hits = text.match(/cabbage/g) (from you)
or
var hits = text.match(/cabbage/gi); (from daksh)

but both results to [ 'cabbage', 'cabbage' ]

Thank you very much for kind reply :slight_smile:


#9

if you want this:

[ 'c', 'a', 'b', 'b', 'a', 'g', 'e', 'c', 'a', 'b', 'b', 'a', 'g', 'e' ]

you should make the programs as taught in the lessons? look at @edwinxc code


#10

in the explanation at this course (on the left pane), it says:

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

So I wonder if I could do the cabbage/g and make the result
[ 'c', 'a', 'b', 'b', 'a', 'g', 'e', 'c', 'a', 'b', 'b', 'a', 'g', 'e' ]

Thank you for your reply :blush:


#11

you could check if the first and last letter are a match?

if (text[i] === "C" && text[i+myName.length-1] === "e")

#12

if (text[i] === "C" && text[i+myName.length-1] === "e")

uwah that is one complicated code >_<

what does it means and where should i put the code?

Thank you for replying :blush:


#13

in the place of the original if condition?

it checks both the first and last letter, the && is a and operator, both conditions have to be true, you could also use a nested if:
`
if (condition1){
if (condition2){
// something like this
}
}


#14

if (text[i] === "C" && text[i+myName.length-1] === "e") {
if (hits===0) {
    console.log("Your name wasn't found!");
} else {
    console.log(hits);
    }
};

I did this and there is no result (black screen only):sweat:


#15

you spelled cabbage with lowercase c, i didn't see that.

then you get this:

/*jshint multistr:true */

var text = "healthy cabbage is healthy, thus the cabbage is healthy.";
var myName = "cabbage";
var hits = [];

for (var i=0;i<text.length;i++) {
    if (text[i]==="c" && text[i+myName.length-1] === "e") {
        for (var j=i;j<(myName.length+i);j++) {
            hits.push(text[j]);
        }
    }
};

if (hits===0) {
    console.log("Your name wasn't found!");
} else {
    var hits = text.match(/cabbage/gi);
    console.log(hits);  
};

#16

I copy pasted the whole new code but still the result

[ 'cabbage', 'cabbage' ]
:cry:


#17

remove this line:

var hits = text.match(/cabbage/gi);

i am too tired today, i am sorry


#18

Cool it worked :persevere:

It's okay, thank you very much, sorry for bothering, I love you stetim:kissing_closed_eyes:


#19

no problem, i should just have noticed it earlier. You can also add more conditions or even use substring() to make sure the name is an exact match


#20

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