Check strings for "!"

You pretty much almost have a working solution if you just change this line to use endsWith() on the original string argument instead (and flip your logic around).

Can you show me how you apply it?

Goddamnit responsibility.

MDN always has examples for things like these: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

1 Like

I just don’t understand what he meant. I need to change that line with endsWith() with the original string? The original string was turned into an array.

EDIT: Why am I using endsWith() when it’s an array?

The original string was not turned into an array—most methods in JavaScript are not ā€œmutatorsā€ and don’t change their arguments, but instead return a value given an argument.

arr = str.split("!");

This line saves the return value from the split() method into the arr variable (you should add the ā€˜var’ declaration on this, btw). It doesn’t change the str variable at all, it just leaves it alone. As I mentioned above, endsWith() should be used on the original string argument (the str variable).

Yes, but this doesn’t work…

function intenseString(str) {
  arr = str.split("!").endsWith("!!");
  if(arr[1] === ""){
      return true;
  } else {
      if(arr[2] === "" || arr.slice(-2) === "")
      return false;
      else
      return true;
  }
}

endsWith() is supposed to be used on strings, not arrays.

What using this function will do is check if the sentence has 1 of the criteria of an intense, which is to make sure that the sentence has at least 3 !s.

1 Like

The endsWith() method can’t be used on arrays so you can’t chain it to the split() method, which returns an array. As I keep saying, you should use it on the str parameter variable, which is a string, in the first line of the ā€˜else’ block:

if (str.endsWith()) // still needs the correct argument to work...

@rstorms You want to use endsWith on the string before you do anything else. There is no sense in going on with the rest of the function if the string does not end with ā€˜!!!’.

You could do something such as:

function intenseString(str) {
	// return false right away if the string does not end with '!!!'
	if (!str.endsWith('!!!')) return false;
	
      // the rest of your code
}

Why would you have !str?

! is the not operator, so that condition is saying ā€œif the string does NOT end with !!!, return falseā€. This is giving us an early exit so the rest of the function does not have to run in cases where it should not.

Why wouldn’t you just say if the string ends with !!!, return true? Just adds more steps, right?

I thought this would be the solution…I was wrong.

function intenseString(str) {
  arr = str.split("!");
  if(str.endsWith("!!!")){
      return true;
  } else {
      if(arr[2] === "" || arr.slice(-2) === "")
      return false;
      else
      return true;
  }
}

You need to delete this line, it’s malformed code and isn’t doing anything.

At this point I recommend taking a break and coming back to it tomorrow.

Rhetorical…how do you make a grown man cry?

Well fucking A I got it:

function intenseString(str) {
  arr = str.split("!");
  if(str.endsWith("!!!")){
      return true;
  } else {
      if(str.endsWith(""))
      return false;
      else
      return true;
  }
}

EDIT: NVM hahahaha of course it said I failed. Well…it worked in the console.

I’m about to start throwing hands.

That code might give you the results you want for a very limited set of tests, but it’s not actually going to work for all cases.

Rather than trying to continue with this problem, I recommend gaining a greater understanding of your larger obstacle, which is programming concepts. I’d recommend going through these courses if you haven’t done them:

I’m seriously this bad at this stuff? Damnit I feel terrible.