Fixing the promiscuity of hits with endsWith()



<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
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++) {
if (hits.length === 0) {
console.log(“Your name wasn’t found!”);
} else if (hits.endsWith(“tron”)) {
return 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?


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.



Is your problem now fixed?


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.


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…