Fixing the promiscuity of hits with endsWith()


#1



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


SyntaxError: Illegal return statement


/*jshint multistr:true */

var text = "Duis libero erat, aliquam ut orci et, faucibus commodo elit. Donec pulvinar dignissim felis interdum auctor. Fusce  Calbotron nibh sem, condimentum nec placerat ac, maximus et felis. Donec id vestibulum odio. Nam molestie orci consectetur, elementum risus id, scelerisque ante. Vivamus faucibus est ut dapibus tincidunt. Suspendisse potenti. Morbi eu enim nec lectus volutpat dignissim. sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Duis volutpat, nibh tincidunt viverra dignissim, ipsum arcu accumsan nibh, sit amet fermentum lacus mi quis nibh. Morbi sed felis et augue iaculis finibus calbotron vitae volutpat orci. urabitur laoreet mauris in dui fringilla consequat. Curabitur Vestibulum viverra commodo efficitur. Suspendisse convallis feugiat turpis ultrices iaculis. In hac habitasse platea dictumst. Sed dapibus justo eu magna gravida, ut blandit lectus vulputate.";
var myName = "Calbotron";
var hits = [];

for (var i = 0; i < text.length; i++) {
    if (text[i] === "C") {
        for (var j = i; j < i + myName.length; j++) {
            hits.push(text[j]);
            //console.log(text[j]);
        }
    }
}
if (hits.length === 0) {
    console.log("Your name wasn't found!");
} else if (hits.endsWith("tron")) {
   return hits;
    console.log(hits);
}
    else {
    console.log("Similar matches but no exact match.");
}

I thought I could possibly fix the search function using endsWith() since if the myName string doesn't end with the last few letters of my name that would filter out the unwanted strings.

I just don't seem to know enough syntax to get this to work though. As it is I get an illegal function error and if I remove the return I get a TypeError: hits.endsWith is not a function error. I wondered if I need yet another variable to use for comparing hits to myName.

Is endsWith() a viable method, and what do I need to fix?


#2

Hi calydon :slight_smile:

This line is wrong:

else if (hits.endsWith("tron"))

as hits is an array (containing strings). endsWith is a method used with strings only. Moreover, this line:

return hits;

is wrong because return goes inside a function, which is nowhere in your code.

Now, coming to what you want to do, you need a combination of filter method and endsWith method, like this:

hits = hits.filter(function(hitString){
    return hitString.endsWith("tron");
});

If that does not make sense, you can read the docs for filter.

HTH!


#3

Is your problem now fixed?


#4

Not exactly - I still don't know how to incorporate what you've shown me into my code. But that's OK, I'm going to leave this for now until I have a better understanding of functions.


#5

Hi @calydon,

I'm not familiar with endsWith outside of what I've read here (yet), but could you add your characters to a string instead of an array? If I'm understanding @gaurangtandon's explanation correctly it seems like that might work...

frisby